ld: Allow section groups to be resolved as part of a relocatable link
[deliverable/binutils-gdb.git] / ld / ldlang.c
index 89b896f51dfe81faa37bd9130a577a228bde0edf..252400bd510d8924c9ea343490d9bdc86f842ac5 100644 (file)
@@ -207,7 +207,7 @@ unique_section_p (const asection *sec,
   struct unique_sections *unam;
   const char *secnam;
 
-  if (bfd_link_relocatable (&link_info)
+  if (!link_info.resolve_section_groups
       && sec->owner != NULL
       && bfd_is_group_section (sec->owner, sec))
     return !(os != NULL
@@ -1138,11 +1138,14 @@ lang_add_input_file (const char *name,
                     lang_input_file_enum_type file_type,
                     const char *target)
 {
-  if (name != NULL && *name == '=')
+  if (name != NULL
+      && (*name == '=' || CONST_STRNEQ (name, "$SYSROOT")))
     {
       lang_input_statement_type *ret;
       char *sysrooted_name
-       = concat (ld_sysroot, name + 1, (const char *) NULL);
+       = concat (ld_sysroot,
+                 name + (*name == '=' ? 1 : strlen ("$SYSROOT")),
+                 (const char *) NULL);
 
       /* We've now forcibly prepended the sysroot, making the input
         file independent of the context.  Therefore, temporarily
@@ -2346,6 +2349,12 @@ lang_add_section (lang_statement_list_type *ptr,
   if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
     discard = TRUE;
 
+  /* Discard the group descriptor sections when we're finally placing the
+     sections from within the group.  */
+  if ((section->flags & SEC_GROUP) == SEC_GROUP
+      && link_info.resolve_section_groups)
+    discard = TRUE;
+
   /* Discard debugging sections if we are stripping debugging
      information.  */
   if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
@@ -2386,8 +2395,14 @@ lang_add_section (lang_statement_list_type *ptr,
      already been processed.  One reason to do this is that on pe
      format targets, .text$foo sections go into .text and it's odd
      to see .text with SEC_LINK_ONCE set.  */
-
-  if (!bfd_link_relocatable (&link_info))
+  if ((flags & (SEC_LINK_ONCE | SEC_GROUP)) == (SEC_LINK_ONCE | SEC_GROUP))
+    {
+      if (link_info.resolve_section_groups)
+        flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC);
+      else
+        flags &= ~(SEC_LINK_DUPLICATES | SEC_RELOC);
+    }
+  else if (!bfd_link_relocatable (&link_info))
     flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC);
 
   switch (output->sectype)
@@ -3429,6 +3444,8 @@ insert_undefined (const char *name)
     {
       h->type = bfd_link_hash_undefined;
       h->u.undef.abfd = NULL;
+      if (is_elf_hash_table (link_info.hash))
+       ((struct elf_link_hash_entry *) h)->mark = 1;
       bfd_link_add_undef (link_info.hash, h);
     }
 }
@@ -4768,6 +4785,7 @@ lang_check_section_addresses (void)
   asection *s, *p;
   struct check_sec *sections;
   size_t i, count;
+  bfd_vma addr_mask;
   bfd_vma s_start;
   bfd_vma s_end;
   bfd_vma p_start = 0;
@@ -4775,6 +4793,26 @@ lang_check_section_addresses (void)
   lang_memory_region_type *m;
   bfd_boolean overlays;
 
+  /* Detect address space overflow on allocated sections.  */
+  addr_mask = ((bfd_vma) 1 <<
+              (bfd_arch_bits_per_address (link_info.output_bfd) - 1)) - 1;
+  addr_mask = (addr_mask << 1) + 1;
+  for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
+    if ((s->flags & SEC_ALLOC) != 0)
+      {
+       s_end = (s->vma + s->size) & addr_mask;
+       if (s_end != 0 && s_end < (s->vma & addr_mask))
+         einfo (_("%X%P: section %s VMA wraps around address space\n"),
+                s->name);
+       else
+         {
+           s_end = (s->lma + s->size) & addr_mask;
+           if (s_end != 0 && s_end < (s->lma & addr_mask))
+             einfo (_("%X%P: section %s LMA wraps around address space\n"),
+                    s->name);
+         }
+      }
+
   if (bfd_count_sections (link_info.output_bfd) <= 1)
     return;
 
@@ -5197,9 +5235,6 @@ lang_size_sections_1
              }
            os->processed_lma = TRUE;
 
-           if (bfd_is_abs_section (os->bfd_section) || os->ignored)
-             break;
-
            /* Keep track of normal sections using the default
               lma region.  We use this to set the lma for
               following sections.  Overlays or other linker
@@ -5207,7 +5242,13 @@ lang_size_sections_1
               default lma == vma is incorrect.
               To avoid warnings about dot moving backwards when using
               -Ttext, don't start tracking sections until we find one
-              of non-zero size or with lma set differently to vma.  */
+              of non-zero size or with lma set differently to vma.
+              Do this tracking before we short-cut the loop so that we
+              track changes for the case where the section size is zero,
+              but the lma is set differently to the vma.  This is
+              important, if an orphan section is placed after an
+              otherwise empty output section that has an explicit lma
+              set, we want that lma reflected in the orphans lma.  */
            if (!IGNORE_SECTION (os->bfd_section)
                && (os->bfd_section->size != 0
                    || (r->last_os == NULL
@@ -5219,6 +5260,9 @@ lang_size_sections_1
                && !bfd_link_relocatable (&link_info))
              r->last_os = s;
 
+           if (bfd_is_abs_section (os->bfd_section) || os->ignored)
+             break;
+
            /* .tbss sections effectively have zero size.  */
            if (!IS_TBSS (os->bfd_section)
                || bfd_link_relocatable (&link_info))
This page took 0.026866 seconds and 4 git commands to generate.