elflink.c revision 68765
1228753Smm/* ELF linking support for BFD. 2228753Smm Copyright 1995, 96, 97, 98, 99, 2000 Free Software Foundation, Inc. 3228753Smm 4228753SmmThis file is part of BFD, the Binary File Descriptor library. 5228753Smm 6228753SmmThis program is free software; you can redistribute it and/or modify 7228753Smmit under the terms of the GNU General Public License as published by 8228753Smmthe Free Software Foundation; either version 2 of the License, or 9228753Smm(at your option) any later version. 10228753Smm 11228753SmmThis program is distributed in the hope that it will be useful, 12228753Smmbut WITHOUT ANY WARRANTY; without even the implied warranty of 13228753SmmMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14228753SmmGNU General Public License for more details. 15228753Smm 16228753SmmYou should have received a copy of the GNU General Public License 17228753Smmalong with this program; if not, write to the Free Software 18228753SmmFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 19228753Smm 20228753Smm#include "bfd.h" 21228753Smm#include "sysdep.h" 22228753Smm#include "bfdlink.h" 23228753Smm#include "libbfd.h" 24228753Smm#define ARCH_SIZE 0 25228753Smm#include "elf-bfd.h" 26228753Smm 27228753Smmboolean 28228753Smm_bfd_elf_create_got_section (abfd, info) 29228753Smm bfd *abfd; 30228753Smm struct bfd_link_info *info; 31228763Smm{ 32228753Smm flagword flags; 33228753Smm register asection *s; 34228753Smm struct elf_link_hash_entry *h; 35228753Smm struct elf_backend_data *bed = get_elf_backend_data (abfd); 36228753Smm int ptralign; 37228753Smm 38228753Smm /* This function may be called more than once. */ 39228753Smm if (bfd_get_section_by_name (abfd, ".got") != NULL) 40228753Smm return true; 41228753Smm 42228753Smm switch (bed->s->arch_size) 43228753Smm { 44228753Smm case 32: ptralign = 2; break; 45228753Smm case 64: ptralign = 3; break; 46228753Smm default: abort(); 47228753Smm } 48228753Smm 49228753Smm flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY 50228753Smm | SEC_LINKER_CREATED); 51228753Smm 52228753Smm s = bfd_make_section (abfd, ".got"); 53228753Smm if (s == NULL 54228753Smm || !bfd_set_section_flags (abfd, s, flags) 55228753Smm || !bfd_set_section_alignment (abfd, s, ptralign)) 56228753Smm return false; 57228753Smm 58228753Smm if (bed->want_got_plt) 59228753Smm { 60228753Smm s = bfd_make_section (abfd, ".got.plt"); 61228753Smm if (s == NULL 62228753Smm || !bfd_set_section_flags (abfd, s, flags) 63228753Smm || !bfd_set_section_alignment (abfd, s, ptralign)) 64228753Smm return false; 65228753Smm } 66228753Smm 67228753Smm /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got 68228753Smm (or .got.plt) section. We don't do this in the linker script 69228753Smm because we don't want to define the symbol if we are not creating 70228753Smm a global offset table. */ 71228753Smm h = NULL; 72228753Smm if (!(_bfd_generic_link_add_one_symbol 73228753Smm (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, 74228753Smm bed->got_symbol_offset, (const char *) NULL, false, 75228753Smm bed->collect, (struct bfd_link_hash_entry **) &h))) 76228753Smm return false; 77228753Smm h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; 78228753Smm h->type = STT_OBJECT; 79228753Smm 80228753Smm if (info->shared 81228753Smm && ! _bfd_elf_link_record_dynamic_symbol (info, h)) 82228753Smm return false; 83228753Smm 84228753Smm elf_hash_table (info)->hgot = h; 85228753Smm 86228753Smm /* The first bit of the global offset table is the header. */ 87228753Smm s->_raw_size += bed->got_header_size + bed->got_symbol_offset; 88228753Smm 89228753Smm return true; 90228753Smm} 91228753Smm 92228753Smm 93228753Smm/* Create dynamic sections when linking against a dynamic object. */ 94228753Smm 95228753Smmboolean 96228753Smm_bfd_elf_create_dynamic_sections (abfd, info) 97228753Smm bfd *abfd; 98228753Smm struct bfd_link_info *info; 99228753Smm{ 100228753Smm flagword flags, pltflags; 101228753Smm register asection *s; 102228753Smm struct elf_backend_data *bed = get_elf_backend_data (abfd); 103228753Smm int ptralign = 0; 104228753Smm 105228753Smm switch (bed->s->arch_size) 106228753Smm { 107228753Smm case 32: ptralign = 2; break; 108228753Smm case 64: ptralign = 3; break; 109228753Smm default: abort(); 110228753Smm } 111228753Smm 112228753Smm /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and 113228753Smm .rel[a].bss sections. */ 114228753Smm 115228753Smm flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY 116228753Smm | SEC_LINKER_CREATED); 117228753Smm 118228753Smm pltflags = flags; 119228753Smm pltflags |= SEC_CODE; 120228753Smm if (bed->plt_not_loaded) 121228753Smm pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS); 122228753Smm if (bed->plt_readonly) 123228753Smm pltflags |= SEC_READONLY; 124228753Smm 125228753Smm s = bfd_make_section (abfd, ".plt"); 126228753Smm if (s == NULL 127228753Smm || ! bfd_set_section_flags (abfd, s, pltflags) 128228753Smm || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) 129228753Smm return false; 130228753Smm 131228753Smm if (bed->want_plt_sym) 132228753Smm { 133228753Smm /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the 134228753Smm .plt section. */ 135228753Smm struct elf_link_hash_entry *h = NULL; 136228753Smm if (! (_bfd_generic_link_add_one_symbol 137228753Smm (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 138228753Smm (bfd_vma) 0, (const char *) NULL, false, 139228753Smm get_elf_backend_data (abfd)->collect, 140228753Smm (struct bfd_link_hash_entry **) &h))) 141228753Smm return false; 142228753Smm h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; 143228753Smm h->type = STT_OBJECT; 144228753Smm 145228753Smm if (info->shared 146228753Smm && ! _bfd_elf_link_record_dynamic_symbol (info, h)) 147228753Smm return false; 148228753Smm } 149228753Smm 150228753Smm s = bfd_make_section (abfd, 151228753Smm bed->default_use_rela_p ? ".rela.plt" : ".rel.plt"); 152228753Smm if (s == NULL 153228753Smm || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) 154228753Smm || ! bfd_set_section_alignment (abfd, s, ptralign)) 155 return false; 156 157 if (! _bfd_elf_create_got_section (abfd, info)) 158 return false; 159 160 if (bed->want_dynbss) 161 { 162 /* The .dynbss section is a place to put symbols which are defined 163 by dynamic objects, are referenced by regular objects, and are 164 not functions. We must allocate space for them in the process 165 image and use a R_*_COPY reloc to tell the dynamic linker to 166 initialize them at run time. The linker script puts the .dynbss 167 section into the .bss section of the final image. */ 168 s = bfd_make_section (abfd, ".dynbss"); 169 if (s == NULL 170 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC)) 171 return false; 172 173 /* The .rel[a].bss section holds copy relocs. This section is not 174 normally needed. We need to create it here, though, so that the 175 linker will map it to an output section. We can't just create it 176 only if we need it, because we will not know whether we need it 177 until we have seen all the input files, and the first time the 178 main linker code calls BFD after examining all the input files 179 (size_dynamic_sections) the input sections have already been 180 mapped to the output sections. If the section turns out not to 181 be needed, we can discard it later. We will never need this 182 section when generating a shared object, since they do not use 183 copy relocs. */ 184 if (! info->shared) 185 { 186 s = bfd_make_section (abfd, 187 (bed->default_use_rela_p 188 ? ".rela.bss" : ".rel.bss")); 189 if (s == NULL 190 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) 191 || ! bfd_set_section_alignment (abfd, s, ptralign)) 192 return false; 193 } 194 } 195 196 return true; 197} 198 199 200/* Record a new dynamic symbol. We record the dynamic symbols as we 201 read the input files, since we need to have a list of all of them 202 before we can determine the final sizes of the output sections. 203 Note that we may actually call this function even though we are not 204 going to output any dynamic symbols; in some cases we know that a 205 symbol should be in the dynamic symbol table, but only if there is 206 one. */ 207 208boolean 209_bfd_elf_link_record_dynamic_symbol (info, h) 210 struct bfd_link_info *info; 211 struct elf_link_hash_entry *h; 212{ 213 if (h->dynindx == -1) 214 { 215 struct bfd_strtab_hash *dynstr; 216 char *p, *alc; 217 const char *name; 218 boolean copy; 219 bfd_size_type indx; 220 221 /* XXX: The ABI draft says the linker must turn hidden and 222 internal symbols into STB_LOCAL symbols when producing the 223 DSO. However, if ld.so honors st_other in the dynamic table, 224 this would not be necessary. */ 225 switch (ELF_ST_VISIBILITY (h->other)) 226 { 227 case STV_INTERNAL: 228 case STV_HIDDEN: 229 if (h->root.type != bfd_link_hash_undefined 230 && h->root.type != bfd_link_hash_undefweak) 231 { 232 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; 233 return true; 234 } 235 236 default: 237 break; 238 } 239 240 h->dynindx = elf_hash_table (info)->dynsymcount; 241 ++elf_hash_table (info)->dynsymcount; 242 243 dynstr = elf_hash_table (info)->dynstr; 244 if (dynstr == NULL) 245 { 246 /* Create a strtab to hold the dynamic symbol names. */ 247 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init (); 248 if (dynstr == NULL) 249 return false; 250 } 251 252 /* We don't put any version information in the dynamic string 253 table. */ 254 name = h->root.root.string; 255 p = strchr (name, ELF_VER_CHR); 256 if (p == NULL) 257 { 258 alc = NULL; 259 copy = false; 260 } 261 else 262 { 263 alc = bfd_malloc (p - name + 1); 264 if (alc == NULL) 265 return false; 266 strncpy (alc, name, p - name); 267 alc[p - name] = '\0'; 268 name = alc; 269 copy = true; 270 } 271 272 indx = _bfd_stringtab_add (dynstr, name, true, copy); 273 274 if (alc != NULL) 275 free (alc); 276 277 if (indx == (bfd_size_type) -1) 278 return false; 279 h->dynstr_index = indx; 280 } 281 282 return true; 283} 284 285/* Return the dynindex of a local dynamic symbol. */ 286 287long 288_bfd_elf_link_lookup_local_dynindx (info, input_bfd, input_indx) 289 struct bfd_link_info *info; 290 bfd *input_bfd; 291 long input_indx; 292{ 293 struct elf_link_local_dynamic_entry *e; 294 295 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) 296 if (e->input_bfd == input_bfd && e->input_indx == input_indx) 297 return e->dynindx; 298 return -1; 299} 300 301/* This function is used to renumber the dynamic symbols, if some of 302 them are removed because they are marked as local. This is called 303 via elf_link_hash_traverse. */ 304 305static boolean elf_link_renumber_hash_table_dynsyms 306 PARAMS ((struct elf_link_hash_entry *, PTR)); 307 308static boolean 309elf_link_renumber_hash_table_dynsyms (h, data) 310 struct elf_link_hash_entry *h; 311 PTR data; 312{ 313 size_t *count = (size_t *) data; 314 315 if (h->dynindx != -1) 316 h->dynindx = ++(*count); 317 318 return true; 319} 320 321/* Assign dynsym indicies. In a shared library we generate a section 322 symbol for each output section, which come first. Next come all of 323 the back-end allocated local dynamic syms, followed by the rest of 324 the global symbols. */ 325 326unsigned long 327_bfd_elf_link_renumber_dynsyms (output_bfd, info) 328 bfd *output_bfd; 329 struct bfd_link_info *info; 330{ 331 unsigned long dynsymcount = 0; 332 333 if (info->shared) 334 { 335 asection *p; 336 for (p = output_bfd->sections; p ; p = p->next) 337 elf_section_data (p)->dynindx = ++dynsymcount; 338 } 339 340 if (elf_hash_table (info)->dynlocal) 341 { 342 struct elf_link_local_dynamic_entry *p; 343 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) 344 p->dynindx = ++dynsymcount; 345 } 346 347 elf_link_hash_traverse (elf_hash_table (info), 348 elf_link_renumber_hash_table_dynsyms, 349 &dynsymcount); 350 351 /* There is an unused NULL entry at the head of the table which 352 we must account for in our count. Unless there weren't any 353 symbols, which means we'll have no table at all. */ 354 if (dynsymcount != 0) 355 ++dynsymcount; 356 357 return elf_hash_table (info)->dynsymcount = dynsymcount; 358} 359 360/* Create a special linker section, or return a pointer to a linker 361 section already created */ 362 363elf_linker_section_t * 364_bfd_elf_create_linker_section (abfd, info, which, defaults) 365 bfd *abfd; 366 struct bfd_link_info *info; 367 enum elf_linker_section_enum which; 368 elf_linker_section_t *defaults; 369{ 370 bfd *dynobj = elf_hash_table (info)->dynobj; 371 elf_linker_section_t *lsect; 372 373 /* Record the first bfd section that needs the special section */ 374 if (!dynobj) 375 dynobj = elf_hash_table (info)->dynobj = abfd; 376 377 /* If this is the first time, create the section */ 378 lsect = elf_linker_section (dynobj, which); 379 if (!lsect) 380 { 381 asection *s; 382 383 lsect = (elf_linker_section_t *) 384 bfd_alloc (dynobj, sizeof (elf_linker_section_t)); 385 386 *lsect = *defaults; 387 elf_linker_section (dynobj, which) = lsect; 388 lsect->which = which; 389 lsect->hole_written_p = false; 390 391 /* See if the sections already exist */ 392 lsect->section = s = bfd_get_section_by_name (dynobj, lsect->name); 393 if (!s || (s->flags & defaults->flags) != defaults->flags) 394 { 395 lsect->section = s = bfd_make_section_anyway (dynobj, lsect->name); 396 397 if (s == NULL) 398 return (elf_linker_section_t *)0; 399 400 bfd_set_section_flags (dynobj, s, defaults->flags); 401 bfd_set_section_alignment (dynobj, s, lsect->alignment); 402 } 403 else if (bfd_get_section_alignment (dynobj, s) < lsect->alignment) 404 bfd_set_section_alignment (dynobj, s, lsect->alignment); 405 406 s->_raw_size = align_power (s->_raw_size, lsect->alignment); 407 408 /* Is there a hole we have to provide? If so check whether the segment is 409 too big already */ 410 if (lsect->hole_size) 411 { 412 lsect->hole_offset = s->_raw_size; 413 s->_raw_size += lsect->hole_size; 414 if (lsect->hole_offset > lsect->max_hole_offset) 415 { 416 (*_bfd_error_handler) (_("%s: Section %s is already to large to put hole of %ld bytes in"), 417 bfd_get_filename (abfd), 418 lsect->name, 419 (long)lsect->hole_size); 420 421 bfd_set_error (bfd_error_bad_value); 422 return (elf_linker_section_t *)0; 423 } 424 } 425 426#ifdef DEBUG 427 fprintf (stderr, "Creating section %s, current size = %ld\n", 428 lsect->name, (long)s->_raw_size); 429#endif 430 431 if (lsect->sym_name) 432 { 433 struct elf_link_hash_entry *h = NULL; 434#ifdef DEBUG 435 fprintf (stderr, "Adding %s to section %s\n", 436 lsect->sym_name, 437 lsect->name); 438#endif 439 h = (struct elf_link_hash_entry *) 440 bfd_link_hash_lookup (info->hash, lsect->sym_name, false, false, false); 441 442 if ((h == NULL || h->root.type == bfd_link_hash_undefined) 443 && !(_bfd_generic_link_add_one_symbol (info, 444 abfd, 445 lsect->sym_name, 446 BSF_GLOBAL, 447 s, 448 ((lsect->hole_size) 449 ? s->_raw_size - lsect->hole_size + lsect->sym_offset 450 : lsect->sym_offset), 451 (const char *) NULL, 452 false, 453 get_elf_backend_data (abfd)->collect, 454 (struct bfd_link_hash_entry **) &h))) 455 return (elf_linker_section_t *)0; 456 457 if ((defaults->which != LINKER_SECTION_SDATA) 458 && (defaults->which != LINKER_SECTION_SDATA2)) 459 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_DYNAMIC; 460 461 h->type = STT_OBJECT; 462 lsect->sym_hash = h; 463 464 if (info->shared 465 && ! _bfd_elf_link_record_dynamic_symbol (info, h)) 466 return (elf_linker_section_t *)0; 467 } 468 } 469 470#if 0 471 /* This does not make sense. The sections which may exist in the 472 object file have nothing to do with the sections we want to 473 create. */ 474 475 /* Find the related sections if they have been created */ 476 if (lsect->bss_name && !lsect->bss_section) 477 lsect->bss_section = bfd_get_section_by_name (dynobj, lsect->bss_name); 478 479 if (lsect->rel_name && !lsect->rel_section) 480 lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name); 481#endif 482 483 return lsect; 484} 485 486 487/* Find a linker generated pointer with a given addend and type. */ 488 489elf_linker_section_pointers_t * 490_bfd_elf_find_pointer_linker_section (linker_pointers, addend, which) 491 elf_linker_section_pointers_t *linker_pointers; 492 bfd_signed_vma addend; 493 elf_linker_section_enum_t which; 494{ 495 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next) 496 { 497 if (which == linker_pointers->which && addend == linker_pointers->addend) 498 return linker_pointers; 499 } 500 501 return (elf_linker_section_pointers_t *)0; 502} 503 504 505/* Make the .rela section corresponding to the generated linker section. */ 506 507boolean 508_bfd_elf_make_linker_section_rela (dynobj, lsect, alignment) 509 bfd *dynobj; 510 elf_linker_section_t *lsect; 511 int alignment; 512{ 513 if (lsect->rel_section) 514 return true; 515 516 lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name); 517 if (lsect->rel_section == NULL) 518 { 519 lsect->rel_section = bfd_make_section (dynobj, lsect->rel_name); 520 if (lsect->rel_section == NULL 521 || ! bfd_set_section_flags (dynobj, 522 lsect->rel_section, 523 (SEC_ALLOC 524 | SEC_LOAD 525 | SEC_HAS_CONTENTS 526 | SEC_IN_MEMORY 527 | SEC_LINKER_CREATED 528 | SEC_READONLY)) 529 || ! bfd_set_section_alignment (dynobj, lsect->rel_section, alignment)) 530 return false; 531 } 532 533 return true; 534} 535