Home | History | Annotate | Line # | Download | only in ld
ldelfgen.c revision 1.1.1.2.2.1
      1 /* Emulation code used by all ELF targets.
      2    Copyright (C) 1991-2024 Free Software Foundation, Inc.
      3 
      4    This file is part of the GNU Binutils.
      5 
      6    This program is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3 of the License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with this program; if not, write to the Free Software
     18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     19    MA 02110-1301, USA.  */
     20 
     21 #include "sysdep.h"
     22 #include "libiberty.h"
     23 #include "bfd.h"
     24 #include "bfdlink.h"
     25 #include "ctf-api.h"
     26 #include "ld.h"
     27 #include "ldmain.h"
     28 #include "ldmisc.h"
     29 #include "ldexp.h"
     30 #include "ldlang.h"
     31 #include "ldctor.h"
     32 #include "elf-bfd.h"
     33 #include "elf/internal.h"
     34 #include "ldelfgen.h"
     35 
     36 /* Info attached to an output_section_statement about input sections,
     37    used when sorting SHF_LINK_ORDER sections.  */
     38 
     39 struct os_sections
     40 {
     41   /* Size allocated for isec.  */
     42   unsigned int alloc;
     43   /* Used entries in isec.  */
     44   unsigned int count;
     45   /* How many are SHF_LINK_ORDER.  */
     46   unsigned int ordered;
     47   /* Input sections attached to this output section.  */
     48   struct os_sections_input {
     49     lang_input_section_type *is;
     50     unsigned int idx;
     51   } isec[1];
     52 };
     53 
     54 /* Add IS to data kept for OS.  */
     55 
     56 static bool
     57 add_link_order_input_section (lang_input_section_type *is,
     58 			      lang_output_section_statement_type *os)
     59 {
     60   struct os_sections *os_info = os->data;
     61   asection *s;
     62 
     63   if (os_info == NULL)
     64     {
     65       os_info = xmalloc (sizeof (*os_info) + 63 * sizeof (*os_info->isec));
     66       os_info->alloc = 64;
     67       os_info->count = 0;
     68       os_info->ordered = 0;
     69       os->data = os_info;
     70     }
     71   if (os_info->count == os_info->alloc)
     72     {
     73       size_t want;
     74       os_info->alloc *= 2;
     75       want = sizeof (*os_info) + (os_info->alloc - 1) * sizeof (*os_info->isec);
     76       os_info = xrealloc (os_info, want);
     77       os->data = os_info;
     78     }
     79   os_info->isec[os_info->count].is = is;
     80   os_info->isec[os_info->count].idx = os_info->count;
     81   os_info->count++;
     82   s = is->section;
     83   if (bfd_get_flavour (s->owner) == bfd_target_elf_flavour
     84       && (s->flags & SEC_LINKER_CREATED) == 0
     85       && elf_linked_to_section (s) != NULL)
     86     os_info->ordered++;
     87   return false;
     88 }
     89 
     90 /* Run over the linker's statement list, extracting info about input
     91    sections attached to each output section.  */
     92 
     93 static bool
     94 link_order_scan (lang_statement_union_type *u,
     95 		 lang_output_section_statement_type *os)
     96 {
     97   asection *s;
     98   bool ret = false;
     99 
    100   for (; u != NULL; u = u->header.next)
    101     {
    102       switch (u->header.type)
    103 	{
    104 	case lang_wild_statement_enum:
    105 	  if (link_order_scan (u->wild_statement.children.head, os))
    106 	    ret = true;
    107 	  break;
    108 	case lang_constructors_statement_enum:
    109 	  if (link_order_scan (constructor_list.head, os))
    110 	    ret = true;
    111 	  break;
    112 	case lang_output_section_statement_enum:
    113 	  if (u->output_section_statement.constraint != -1
    114 	      && link_order_scan (u->output_section_statement.children.head,
    115 				  &u->output_section_statement))
    116 	    ret = true;
    117 	  break;
    118 	case lang_group_statement_enum:
    119 	  if (link_order_scan (u->group_statement.children.head, os))
    120 	    ret = true;
    121 	  break;
    122 	case lang_input_section_enum:
    123 	  s = u->input_section.section;
    124 	  if (s->output_section != NULL
    125 	      && s->output_section->owner == link_info.output_bfd
    126 	      && (s->output_section->flags & SEC_EXCLUDE) == 0
    127 	      && ((s->output_section->flags & SEC_HAS_CONTENTS) != 0
    128 		  || ((s->output_section->flags & (SEC_LOAD | SEC_THREAD_LOCAL))
    129 		      == (SEC_LOAD | SEC_THREAD_LOCAL))))
    130 	    if (add_link_order_input_section (&u->input_section, os))
    131 	      ret = true;
    132 	  break;
    133 	default:
    134 	  break;
    135 	}
    136     }
    137   return ret;
    138 }
    139 
    140 /* Compare two sections based on the locations of the sections they are
    141    linked to.  Used by fixup_link_order.  */
    142 
    143 static int
    144 compare_link_order (const void *a, const void *b)
    145 {
    146   const struct os_sections_input *ai = a;
    147   const struct os_sections_input *bi = b;
    148   asection *asec = NULL;
    149   asection *bsec = NULL;
    150   bfd_vma apos, bpos;
    151 
    152   if (bfd_get_flavour (ai->is->section->owner) == bfd_target_elf_flavour)
    153     asec = elf_linked_to_section (ai->is->section);
    154   if (bfd_get_flavour (bi->is->section->owner) == bfd_target_elf_flavour)
    155     bsec = elf_linked_to_section (bi->is->section);
    156 
    157   /* Place unordered sections before ordered sections.  */
    158   if (asec == NULL || bsec == NULL)
    159     {
    160       if (bsec != NULL)
    161 	return -1;
    162       else if (asec != NULL)
    163 	return 1;
    164       return ai->idx - bi->idx;
    165     }
    166 
    167   apos = asec->output_section->lma + asec->output_offset;
    168   bpos = bsec->output_section->lma + bsec->output_offset;
    169 
    170   if (apos < bpos)
    171     return -1;
    172   else if (apos > bpos)
    173     return 1;
    174 
    175   if (! bfd_link_relocatable (&link_info))
    176     {
    177       /* The only way we should get matching LMAs is when the first of
    178 	 the two sections has zero size, or asec and bsec are the
    179 	 same section.  */
    180       if (asec->size < bsec->size)
    181 	return -1;
    182       else if (asec->size > bsec->size)
    183 	return 1;
    184     }
    185 
    186   /* If they are both zero size then they almost certainly have the same
    187      VMA and thus are not ordered with respect to each other.  Test VMA
    188      anyway, and fall back to idx to make the result reproducible across
    189      qsort implementations.  */
    190   apos = asec->output_section->vma + asec->output_offset;
    191   bpos = bsec->output_section->vma + bsec->output_offset;
    192   if (apos < bpos)
    193     return -1;
    194   else if (apos > bpos)
    195     return 1;
    196   else
    197     return ai->idx - bi->idx;
    198 }
    199 
    200 /* Rearrange sections with SHF_LINK_ORDER into the same order as their
    201    linked sections.  */
    202 
    203 static bool
    204 fixup_link_order (lang_output_section_statement_type *os)
    205 {
    206   struct os_sections *os_info = os->data;
    207   unsigned int i, j;
    208   lang_input_section_type **orig_is;
    209   asection **save_s;
    210 
    211   for (i = 0; i < os_info->count; i = j)
    212     {
    213       /* Normally a linker script will select SHF_LINK_ORDER sections
    214 	 with an input section wildcard something like the following:
    215 	 *(.IA_64.unwind* .gnu.linkonce.ia64unw.*)
    216 	 However if some other random sections are smashed into an
    217 	 output section, or if SHF_LINK_ORDER are split up by the
    218 	 linker script, then we only want to sort sections matching a
    219 	 given wildcard.  That's the purpose of the pattern test.  */
    220       for (j = i + 1; j < os_info->count; j++)
    221 	if (os_info->isec[j].is->pattern != os_info->isec[i].is->pattern)
    222 	  break;
    223       if (j - i > 1)
    224 	qsort (&os_info->isec[i], j - i, sizeof (*os_info->isec),
    225 	       compare_link_order);
    226     }
    227   for (i = 0; i < os_info->count; i++)
    228     if (os_info->isec[i].idx != i)
    229       break;
    230   if (i == os_info->count)
    231     return false;
    232 
    233   /* Now reorder the linker input section statements to reflect the
    234      proper sorting.  The is done by rewriting the existing statements
    235      rather than fiddling with lists, since the only thing we need to
    236      change is the bfd section pointer.  */
    237   orig_is = xmalloc (os_info->count * sizeof (*orig_is));
    238   save_s = xmalloc (os_info->count * sizeof (*save_s));
    239   for (i = 0; i < os_info->count; i++)
    240     {
    241       orig_is[os_info->isec[i].idx] = os_info->isec[i].is;
    242       save_s[i] = os_info->isec[i].is->section;
    243     }
    244   for (i = 0; i < os_info->count; i++)
    245     if (os_info->isec[i].idx != i)
    246       {
    247 	orig_is[i]->section = save_s[i];
    248 	/* Restore os_info to pristine state before the qsort, for the
    249 	   next pass over sections.  */
    250 	os_info->isec[i].is = orig_is[i];
    251 	os_info->isec[i].idx = i;
    252       }
    253   free (save_s);
    254   free (orig_is);
    255   return true;
    256 }
    257 
    258 void
    259 ldelf_map_segments (bool need_layout)
    260 {
    261   int tries = 10;
    262   static bool done_link_order_scan = false;
    263 
    264   do
    265     {
    266       lang_relax_sections (need_layout);
    267       need_layout = false;
    268 
    269       if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
    270 	{
    271 	  lang_output_section_statement_type *os;
    272 	  if (!done_link_order_scan)
    273 	    {
    274 	      link_order_scan (statement_list.head, NULL);
    275 	      done_link_order_scan = true;
    276 	    }
    277 	  for (os = (void *) lang_os_list.head; os != NULL; os = os->next)
    278 	    {
    279 	      struct os_sections *os_info = os->data;
    280 	      if (os_info != NULL && os_info->ordered != 0)
    281 		{
    282 		  if (os_info->ordered != os_info->count
    283 		      && bfd_link_relocatable (&link_info))
    284 		    {
    285 		      einfo (_("%F%P: "
    286 			       "%pA has both ordered and unordered sections\n"),
    287 			     os->bfd_section);
    288 		      return;
    289 		    }
    290 		  if (os_info->count > 1
    291 		      && fixup_link_order (os))
    292 		    need_layout = true;
    293 		}
    294 	    }
    295 	}
    296 
    297       if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
    298 	  && !bfd_link_relocatable (&link_info))
    299 	{
    300 	  bfd_size_type phdr_size;
    301 
    302 	  phdr_size = elf_program_header_size (link_info.output_bfd);
    303 	  /* If we don't have user supplied phdrs, throw away any
    304 	     previous linker generated program headers.  */
    305 	  if (lang_phdr_list == NULL)
    306 	    elf_seg_map (link_info.output_bfd) = NULL;
    307 	  if (!_bfd_elf_map_sections_to_segments (link_info.output_bfd,
    308 						  &link_info,
    309 						  &need_layout))
    310 	    einfo (_("%F%P: map sections to segments failed: %E\n"));
    311 
    312 	  if (phdr_size != elf_program_header_size (link_info.output_bfd))
    313 	    {
    314 	      if (tries > 6)
    315 		/* The first few times we allow any change to
    316 		   phdr_size .  */
    317 		need_layout = true;
    318 	      else if (phdr_size
    319 		       < elf_program_header_size (link_info.output_bfd))
    320 		/* After that we only allow the size to grow.  */
    321 		need_layout = true;
    322 	      else
    323 		elf_program_header_size (link_info.output_bfd) = phdr_size;
    324 	    }
    325 	}
    326     }
    327   while (need_layout && --tries);
    328 
    329   if (tries == 0)
    330     einfo (_("%F%P: looping in map_segments\n"));
    331 
    332   if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
    333       && lang_phdr_list == NULL)
    334     {
    335       /* If we don't have user supplied phdrs, strip zero-sized dynamic
    336 	 sections and regenerate program headers.  */
    337       const struct elf_backend_data *bed
    338 	= get_elf_backend_data (link_info.output_bfd);
    339       if (bed->elf_backend_strip_zero_sized_dynamic_sections
    340 	  && !bed->elf_backend_strip_zero_sized_dynamic_sections
    341 		(&link_info))
    342 	  einfo (_("%F%P: failed to strip zero-sized dynamic sections\n"));
    343     }
    344 }
    345 
    346 #ifdef ENABLE_LIBCTF
    347 /* We want to emit CTF early if and only if we are not targetting ELF with this
    348    invocation.  */
    349 
    350 int
    351 ldelf_emit_ctf_early (void)
    352 {
    353   if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
    354     return 0;
    355   return 1;
    356 }
    357 
    358 /* Callbacks used to map from bfd types to libctf types, under libctf's
    359    control.  */
    360 
    361 struct ctf_strtab_iter_cb_arg
    362 {
    363   struct elf_strtab_hash *strtab;
    364   size_t next_i;
    365   size_t next_idx;
    366 };
    367 
    368 /* Return strings from the strtab to libctf, one by one.  Returns NULL when
    369    iteration is complete.  */
    370 
    371 static const char *
    372 ldelf_ctf_strtab_iter_cb (uint32_t *offset, void *arg_)
    373 {
    374   bfd_size_type off;
    375   const char *ret;
    376 
    377   struct ctf_strtab_iter_cb_arg *arg =
    378     (struct ctf_strtab_iter_cb_arg *) arg_;
    379 
    380   /* There is no zeroth string.  */
    381   if (arg->next_i == 0)
    382     arg->next_i = 1;
    383 
    384   /* Hunt through strings until we fall off the end or find one with
    385      a nonzero refcount.  */
    386   do
    387     {
    388       if (arg->next_i >= _bfd_elf_strtab_len (arg->strtab))
    389 	{
    390 	  arg->next_i = 0;
    391 	  return NULL;
    392 	}
    393 
    394       ret = _bfd_elf_strtab_str (arg->strtab, arg->next_i++, &off);
    395     }
    396   while (ret == NULL);
    397 
    398   *offset = off;
    399 
    400   /* If we've overflowed, we cannot share any further strings: the CTF
    401      format cannot encode strings with such high offsets.  */
    402   if (*offset != off)
    403     return NULL;
    404 
    405   return ret;
    406 }
    407 
    408 void
    409 ldelf_acquire_strings_for_ctf
    410   (struct ctf_dict *ctf_output, struct elf_strtab_hash *strtab)
    411 {
    412   struct ctf_strtab_iter_cb_arg args = { strtab, 0, 0 };
    413   if (!ctf_output)
    414     return;
    415 
    416   if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
    417     {
    418       if (ctf_link_add_strtab (ctf_output, ldelf_ctf_strtab_iter_cb,
    419 			       &args) < 0)
    420 	einfo (_("%F%P: warning: CTF strtab association failed; strings will "
    421 		 "not be shared: %s\n"),
    422 	       ctf_errmsg (ctf_errno (ctf_output)));
    423     }
    424 }
    425 
    426 void
    427 ldelf_new_dynsym_for_ctf (struct ctf_dict *ctf_output, int symidx,
    428 			  struct elf_internal_sym *sym)
    429 {
    430   ctf_link_sym_t lsym;
    431 
    432   if (!ctf_output)
    433      return;
    434 
    435   /* New symbol.  */
    436   if (sym != NULL)
    437     {
    438       lsym.st_name = NULL;
    439       lsym.st_nameidx = sym->st_name;
    440       lsym.st_nameidx_set = 1;
    441       lsym.st_symidx = symidx;
    442       lsym.st_shndx = sym->st_shndx;
    443       lsym.st_type = ELF_ST_TYPE (sym->st_info);
    444       lsym.st_value = sym->st_value;
    445       if (ctf_link_add_linker_symbol (ctf_output, &lsym) < 0)
    446 	{
    447 	  einfo (_("%F%P: warning: CTF symbol addition failed; CTF will "
    448 		   "not be tied to symbols: %s\n"),
    449 		 ctf_errmsg (ctf_errno (ctf_output)));
    450 	}
    451     }
    452   else
    453     {
    454       /* Shuffle all the symbols.  */
    455 
    456       if (ctf_link_shuffle_syms (ctf_output) < 0)
    457 	einfo (_("%F%P: warning: CTF symbol shuffling failed; CTF will "
    458 		 "not be tied to symbols: %s\n"),
    459 	       ctf_errmsg (ctf_errno (ctf_output)));
    460     }
    461 }
    462 #else
    463 int
    464 ldelf_emit_ctf_early (void)
    465 {
    466   return 0;
    467 }
    468 
    469 void
    470 ldelf_acquire_strings_for_ctf (struct ctf_dict *ctf_output ATTRIBUTE_UNUSED,
    471 			       struct elf_strtab_hash *strtab ATTRIBUTE_UNUSED)
    472 {}
    473 void
    474 ldelf_new_dynsym_for_ctf (struct ctf_dict *ctf_output ATTRIBUTE_UNUSED,
    475 			  int symidx ATTRIBUTE_UNUSED,
    476 			  struct elf_internal_sym *sym ATTRIBUTE_UNUSED)
    477 {}
    478 #endif
    479