Blame SOURCES/binutils-section-type.patch

5044c7
diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
5044c7
index 3b2a4f49a9b..78a0a1dea42 100644
5044c7
--- a/bfd/bfd-in2.h
5044c7
+++ b/bfd/bfd-in2.h
5044c7
@@ -1170,6 +1170,9 @@ typedef struct bfd_section
5044c7
     This is used when support for non-contiguous memory regions is enabled.  */
5044c7
  struct bfd_section *already_assigned;
5044c7
 
5044c7
+  /* Explicitly specified section type, if non-zero. */
5044c7
+  unsigned int type;
5044c7
+
5044c7
 } asection;
5044c7
 
5044c7
 /* Relax table contains information about instructions which can
5044c7
@@ -1352,8 +1355,8 @@ discarded_section (const asection *sec)
5044c7
   /* symbol,                    symbol_ptr_ptr,                     */ \
5044c7
      (struct bfd_symbol *) SYM, &SEC.symbol,                           \
5044c7
                                                                        \
5044c7
-  /* map_head, map_tail, already_assigned                           */ \
5044c7
-     { NULL }, { NULL }, NULL                                          \
5044c7
+  /* map_head, map_tail, already_assigned, type                     */ \
5044c7
+     { NULL }, { NULL }, NULL, 0                                       \
5044c7
                                                                        \
5044c7
     }
5044c7
 
5044c7
diff --git a/bfd/elf.c b/bfd/elf.c
5044c7
index a67415e76e1..82b53be99f9 100644
5044c7
--- a/bfd/elf.c
5044c7
+++ b/bfd/elf.c
5044c7
@@ -3280,7 +3280,9 @@ elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
5044c7
 
5044c7
   /* If the section type is unspecified, we set it based on
5044c7
      asect->flags.  */
5044c7
-  if ((asect->flags & SEC_GROUP) != 0)
5044c7
+  if (asect->type != 0)
5044c7
+    sh_type = asect->type;
5044c7
+  else if ((asect->flags & SEC_GROUP) != 0)
5044c7
     sh_type = SHT_GROUP;
5044c7
   else
5044c7
     sh_type = bfd_elf_get_default_section_type (asect->flags);
5044c7
diff --git a/bfd/section.c b/bfd/section.c
5044c7
index 899438a1c5e..2de7dbf661a 100644
5044c7
--- a/bfd/section.c
5044c7
+++ b/bfd/section.c
5044c7
@@ -737,8 +737,8 @@ CODE_FRAGMENT
5044c7
 .  {* symbol,                    symbol_ptr_ptr,                     *}	\
5044c7
 .     (struct bfd_symbol *) SYM, &SEC.symbol,				\
5044c7
 .									\
5044c7
-.  {* map_head, map_tail, already_assigned                           *}	\
5044c7
-.     { NULL }, { NULL }, NULL						\
5044c7
+.  {* map_head, map_tail, already_assigned, type                     *}	\
5044c7
+.     { NULL }, { NULL }, NULL, 0						\
5044c7
 .									\
5044c7
 .    }
5044c7
 .
5044c7
diff --git a/ld/NEWS b/ld/NEWS
5044c7
index dbb402d1f8a..a498abaf0f9 100644
5044c7
--- a/ld/NEWS
5044c7
+++ b/ld/NEWS
5044c7
@@ -1,5 +1,8 @@
5044c7
 -*- text -*-
5044c7
 
5044c7
+* TYPE=<type> is now supported in an output section description to set the
5044c7
+  section type value.
5044c7
+
5044c7
 Changes in 2.38:
5044c7
 
5044c7
 * Add -z pack-relative-relocs/-z no pack-relative-relocs to x86 ELF
5044c7
diff --git a/ld/ld.texi b/ld/ld.texi
5044c7
index fc75e9b3625..d57e9221410 100644
5044c7
--- a/ld/ld.texi
5044c7
+++ b/ld/ld.texi
5044c7
@@ -5483,13 +5483,23 @@ loaded into memory when the program is run.
5044c7
 @item READONLY
5044c7
 The section should be marked as read-only.
5044c7
 @item DSECT
5044c7
-@itemx COPY
5044c7
-@itemx INFO
5044c7
-@itemx OVERLAY
5044c7
+@item COPY
5044c7
+@item INFO
5044c7
+@item OVERLAY
5044c7
 These type names are supported for backward compatibility, and are
5044c7
 rarely used.  They all have the same effect: the section should be
5044c7
 marked as not allocatable, so that no memory is allocated for the
5044c7
 section when the program is run.
5044c7
+@item TYPE = @var{type}
5044c7
+Set the section type to the integer @var{type}. When generating an ELF
5044c7
+output file, type names @code{SHT_PROGBITS}, @code{SHT_STRTAB},
5044c7
+@code{SHT_NOTE}, @code {SHT_NOBITS}, @code{SHT_INIT_ARRAY},
5044c7
+@code{SHT_FINI_ARRAY}, and @code{SHT_PREINIT_ARRAY} are also allowed
5044c7
+for @var{type}.  It is the user's responsibility to ensure that any
5044c7
+special requirements of the section type are met.
5044c7
+@item READONLY ( TYPE = @var{type} )
5044c7
+This form of the syntax combines the @var{READONLY} type with the
5044c7
+type specified by @var{type}.
5044c7
 @end table
5044c7
 
5044c7
 @kindex NOLOAD
5044c7
diff --git a/ld/ldgram.y b/ld/ldgram.y
5044c7
index 11c2f219c05..3a904e39482 100644
5044c7
--- a/ld/ldgram.y
5044c7
+++ b/ld/ldgram.y
5044c7
@@ -47,6 +47,7 @@
5044c7
 #endif
5044c7
 
5044c7
 static enum section_type sectype;
5044c7
+static etree_type *sectype_value;
5044c7
 static lang_memory_region_type *region;
5044c7
 
5044c7
 static bool ldgram_had_keep = false;
5044c7
@@ -139,6 +140,7 @@ static int error_index;
5044c7
 %token LD_FEATURE
5044c7
 %token NOLOAD DSECT COPY INFO OVERLAY
5044c7
 %token READONLY
5044c7
+%token TYPE
5044c7
 %token DEFINED TARGET_K SEARCH_DIR MAP ENTRY
5044c7
 %token <integer> NEXT
5044c7
 %token SIZEOF ALIGNOF ADDR LOADADDR MAX_K MIN_K
5044c7
@@ -1058,9 +1060,8 @@ section:	NAME
5044c7
 			{
5044c7
 			  ldlex_popstate ();
5044c7
 			  ldlex_wild ();
5044c7
-			  lang_enter_output_section_statement($1, $3, sectype,
5044c7
-							      $5, $7, $4,
5044c7
-							      $8, $6);
5044c7
+			  lang_enter_output_section_statement ($1, $3, sectype,
5044c7
+					sectype_value, $5, $7, $4, $8, $6);
5044c7
 			}
5044c7
 		'{'
5044c7
 		statement_list_opt
5044c7
@@ -1130,8 +1131,10 @@ type:
5044c7
 	|  COPY    { sectype = noalloc_section; }
5044c7
 	|  INFO    { sectype = noalloc_section; }
5044c7
 	|  OVERLAY { sectype = noalloc_section; }
5044c7
+        |  READONLY '(' TYPE '=' exp ')' { sectype = typed_readonly_section; sectype_value = $5; }
5044c7
 	|  READONLY { sectype = readonly_section; }
5044c7
-	;
5044c7
+	|  TYPE '=' exp { sectype = type_section; sectype_value = $3; }
5044c7
+        ;
5044c7
 
5044c7
 atype:
5044c7
 		'(' type ')'
5044c7
diff --git a/ld/ldlang.c b/ld/ldlang.c
5044c7
index 474784c874a..1733f8e65c4 100644
5044c7
--- a/ld/ldlang.c
5044c7
+++ b/ld/ldlang.c
5044c7
@@ -1891,8 +1891,8 @@ lang_insert_orphan (asection *s,
5044c7
     address = exp_intop (0);
5044c7
 
5044c7
   os_tail = (lang_output_section_statement_type **) lang_os_list.tail;
5044c7
-  os = lang_enter_output_section_statement (secname, address, normal_section,
5044c7
-					    NULL, NULL, NULL, constraint, 0);
5044c7
+  os = lang_enter_output_section_statement (
5044c7
+      secname, address, normal_section, 0, NULL, NULL, NULL, constraint, 0);
5044c7
 
5044c7
   if (add_child == NULL)
5044c7
     add_child = &os->children;
5044c7
@@ -2635,10 +2635,12 @@ lang_add_section (lang_statement_list_type *ptr,
5044c7
     case normal_section:
5044c7
     case overlay_section:
5044c7
     case first_overlay_section:
5044c7
+    case type_section:
5044c7
       break;
5044c7
     case noalloc_section:
5044c7
       flags &= ~SEC_ALLOC;
5044c7
       break;
5044c7
+    case typed_readonly_section:
5044c7
     case readonly_section:
5044c7
       flags |= SEC_READONLY;
5044c7
       break;
5044c7
@@ -4209,6 +4211,7 @@ map_input_to_output_sections
5044c7
     {
5044c7
       lang_output_section_statement_type *tos;
5044c7
       flagword flags;
5044c7
+      unsigned int type = 0;
5044c7
 
5044c7
       switch (s->header.type)
5044c7
 	{
5044c7
@@ -4264,6 +4267,42 @@ map_input_to_output_sections
5044c7
 	    case readonly_section:
5044c7
 	      flags |= SEC_READONLY;
5044c7
 	      break;
5044c7
+	    case typed_readonly_section:
5044c7
+	      flags |= SEC_READONLY;
5044c7
+	      /* Fall through.  */
5044c7
+	    case type_section:
5044c7
+	      if (os->sectype_value->type.node_class == etree_name
5044c7
+		  && os->sectype_value->type.node_code == NAME)
5044c7
+		{
5044c7
+		  const char *name = os->sectype_value->name.name;
5044c7
+		  if (strcmp (name, "SHT_PROGBITS") == 0)
5044c7
+		    type = SHT_PROGBITS;
5044c7
+		  else if (strcmp (name, "SHT_STRTAB") == 0)
5044c7
+		    type = SHT_STRTAB;
5044c7
+		  else if (strcmp (name, "SHT_NOTE") == 0)
5044c7
+		    type = SHT_NOTE;
5044c7
+		  else if (strcmp (name, "SHT_NOBITS") == 0)
5044c7
+		    type = SHT_NOBITS;
5044c7
+		  else if (strcmp (name, "SHT_INIT_ARRAY") == 0)
5044c7
+		    type = SHT_INIT_ARRAY;
5044c7
+		  else if (strcmp (name, "SHT_FINI_ARRAY") == 0)
5044c7
+		    type = SHT_FINI_ARRAY;
5044c7
+		  else if (strcmp (name, "SHT_PREINIT_ARRAY") == 0)
5044c7
+		    type = SHT_PREINIT_ARRAY;
5044c7
+		  else
5044c7
+		    einfo (_ ("%F%P: invalid type for output section `%s'\n"),
5044c7
+			   os->name);
5044c7
+		}
5044c7
+	     else
5044c7
+	       {
5044c7
+		 exp_fold_tree_no_dot (os->sectype_value);
5044c7
+		 if (expld.result.valid_p)
5044c7
+		   type = expld.result.value;
5044c7
+		 else
5044c7
+		   einfo (_ ("%F%P: invalid type for output section `%s'\n"),
5044c7
+			  os->name);
5044c7
+	       }
5044c7
+	      break;
5044c7
 	    case noload_section:
5044c7
 	      if (bfd_get_flavour (link_info.output_bfd)
5044c7
 		  == bfd_target_elf_flavour)
5044c7
@@ -4276,6 +4315,7 @@ map_input_to_output_sections
5044c7
 	    init_os (os, flags | SEC_READONLY);
5044c7
 	  else
5044c7
 	    os->bfd_section->flags |= flags;
5044c7
+	  os->bfd_section->type = type;
5044c7
 	  break;
5044c7
 	case lang_input_section_enum:
5044c7
 	  break;
5044c7
@@ -7506,6 +7546,7 @@ lang_output_section_statement_type *
5044c7
 lang_enter_output_section_statement (const char *output_section_statement_name,
5044c7
 				     etree_type *address_exp,
5044c7
 				     enum section_type sectype,
5044c7
+				     etree_type *sectype_value,
5044c7
 				     etree_type *align,
5044c7
 				     etree_type *subalign,
5044c7
 				     etree_type *ebase,
5044c7
@@ -7523,10 +7564,12 @@ lang_enter_output_section_statement (const char *output_section_statement_name,
5044c7
       os->addr_tree = address_exp;
5044c7
     }
5044c7
   os->sectype = sectype;
5044c7
-  if (sectype != noload_section)
5044c7
-    os->flags = SEC_NO_FLAGS;
5044c7
-  else
5044c7
+  if (sectype == type_section || sectype == typed_readonly_section)
5044c7
+    os->sectype_value = sectype_value;
5044c7
+  else if (sectype == noload_section)
5044c7
     os->flags = SEC_NEVER_LOAD;
5044c7
+  else
5044c7
+    os->flags = SEC_NO_FLAGS;
5044c7
   os->block_value = 1;
5044c7
 
5044c7
   /* Make next things chain into subchain of this.  */
5044c7
@@ -8842,7 +8885,7 @@ lang_enter_overlay_section (const char *name)
5044c7
   etree_type *size;
5044c7
 
5044c7
   lang_enter_output_section_statement (name, overlay_vma, overlay_section,
5044c7
-				       0, overlay_subalign, 0, 0, 0);
5044c7
+				       0, 0, overlay_subalign, 0, 0, 0);
5044c7
 
5044c7
   /* If this is the first section, then base the VMA of future
5044c7
      sections on this one.  This will work correctly even if `.' is
5044c7
diff --git a/ld/ldlang.h b/ld/ldlang.h
5044c7
index 0d057c9bee9..95f6e468b30 100644
5044c7
--- a/ld/ldlang.h
5044c7
+++ b/ld/ldlang.h
5044c7
@@ -122,7 +122,9 @@ enum section_type
5044c7
   overlay_section,
5044c7
   noload_section,
5044c7
   noalloc_section,
5044c7
-  readonly_section
5044c7
+  type_section,
5044c7
+  readonly_section,
5044c7
+  typed_readonly_section
5044c7
 };
5044c7
 
5044c7
 /* This structure holds a list of program headers describing
5044c7
@@ -166,6 +168,7 @@ typedef struct lang_output_section_statement_struct
5044c7
   int constraint;
5044c7
   flagword flags;
5044c7
   enum section_type sectype;
5044c7
+  etree_type *sectype_value;
5044c7
   unsigned int processed_vma : 1;
5044c7
   unsigned int processed_lma : 1;
5044c7
   unsigned int all_input_readonly : 1;
5044c7
@@ -545,7 +548,7 @@ extern void lang_add_output
5044c7
   (const char *, int from_script);
5044c7
 extern lang_output_section_statement_type *lang_enter_output_section_statement
5044c7
   (const char *, etree_type *, enum section_type, etree_type *, etree_type *,
5044c7
-   etree_type *, int, int);
5044c7
+   etree_type *, etree_type *, int, int);
5044c7
 extern void lang_final
5044c7
   (void);
5044c7
 extern void lang_relax_sections
5044c7
diff --git a/ld/ldlex.l b/ld/ldlex.l
5044c7
index 78db16e3a48..c38b46b9336 100644
5044c7
--- a/ld/ldlex.l
5044c7
+++ b/ld/ldlex.l
5044c7
@@ -323,6 +323,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
5044c7
 <EXPRESSION>"DSECT"			{ RTOKEN(DSECT); }
5044c7
 <EXPRESSION>"COPY"			{ RTOKEN(COPY); }
5044c7
 <EXPRESSION>"INFO"			{ RTOKEN(INFO); }
5044c7
+<EXPRESSION>"TYPE"			{ RTOKEN(TYPE); }
5044c7
 <SCRIPT,EXPRESSION>"ONLY_IF_RO"		{ RTOKEN(ONLY_IF_RO); }
5044c7
 <SCRIPT,EXPRESSION>"ONLY_IF_RW"		{ RTOKEN(ONLY_IF_RW); }
5044c7
 <SCRIPT,EXPRESSION>"SPECIAL"		{ RTOKEN(SPECIAL); }
5044c7
diff --git a/ld/mri.c b/ld/mri.c
5044c7
index b428ab0d0bf..5749870ef1e 100644
5044c7
--- a/ld/mri.c
5044c7
+++ b/ld/mri.c
5044c7
@@ -210,8 +210,8 @@ mri_draw_tree (void)
5044c7
 	    base = p->vma ? p->vma : exp_nameop (NAME, ".");
5044c7
 
5044c7
 	  lang_enter_output_section_statement (p->name, base,
5044c7
-					       p->ok_to_load ? normal_section : noload_section,
5044c7
-					       align, subalign, NULL, 0, 0);
5044c7
+	    p->ok_to_load ? normal_section : noload_section, 0,
5044c7
+	    align, subalign, NULL, 0, 0);
5044c7
 	  base = 0;
5044c7
 	  tmp = (struct wildcard_list *) xmalloc (sizeof *tmp);
5044c7
 	  tmp->next = NULL;
5044c7
diff --git a/ld/testsuite/ld-scripts/output-section-types.d b/ld/testsuite/ld-scripts/output-section-types.d
5044c7
index ab124fa4dd7..2ecacaba57d 100644
5044c7
--- a/ld/testsuite/ld-scripts/output-section-types.d
5044c7
+++ b/ld/testsuite/ld-scripts/output-section-types.d
5044c7
@@ -1,13 +1,17 @@
5044c7
 #ld: -Toutput-section-types.t
5044c7
 #source: align2a.s
5044c7
-#objdump: -h
5044c7
+#readelf: -S --wide
5044c7
 #target: [is_elf_format]
5044c7
 
5044c7
 #...
5044c7
-  . \.rom.*
5044c7
-[ 	]+ALLOC, READONLY
5044c7
-  . \.ro.*
5044c7
-[ 	]+CONTENTS, ALLOC, LOAD, READONLY, DATA
5044c7
-  . \.over.*
5044c7
-[ 	]+CONTENTS, READONLY
5044c7
+.* .rom          +NOBITS        +[0-9a-f]+ +[0-9a-f]+ +[0-9a-f]+ +00 +A +0 +0 +[1248]
5044c7
+.* .ro           +PROGBITS      +[0-9a-f]+ +[0-9a-f]+ +[0-9a-f]+ +00 +A +0 +0 +[1248]
5044c7
+.* .over         +PROGBITS      +[0-9a-f]+ +[0-9a-f]+ +[0-9a-f]+ +00  + +0 +0 +[1248]
5044c7
+.* progbits      +PROGBITS      +[0-9a-f]+ +[0-9a-f]+ +[0-9a-f]+ +00 +A +0 +0 +[1248]
5044c7
+.* strtab        +STRTAB        +[0-9a-f]+ +[0-9a-f]+ +[0-9a-f]+ +00 +A +0 +0 +[1248]
5044c7
+.* note          +NOTE          +[0-9a-f]+ +[0-9a-f]+ +[0-9a-f]+ +00 +A +0 +0 +[1248]
5044c7
+.* init_array    +INIT_ARRAY    +[0-9a-f]+ +[0-9a-f]+ +[0-9a-f]+ +0[48] +A +0 +0 +[1248]
5044c7
+.* fini_array    +FINI_ARRAY    +[0-9a-f]+ +[0-9a-f]+ +[0-9a-f]+ +0[48] +A +0 +0 +[1248]
5044c7
+.* preinit_array +PREINIT_ARRAY +[0-9a-f]+ +[0-9a-f]+ +[0-9a-f]+ +0[48] +A +0 +0 +[1248]
5044c7
+.* .ro.note      +NOTE          +[0-9a-f]+ +[0-9a-f]+ +[0-9a-f]+ +00 +A +0 +0 +[1248]
5044c7
 #pass
5044c7
diff --git a/ld/testsuite/ld-scripts/output-section-types.t b/ld/testsuite/ld-scripts/output-section-types.t
5044c7
index d8fdfda1a03..18fc5c11980 100644
5044c7
--- a/ld/testsuite/ld-scripts/output-section-types.t
5044c7
+++ b/ld/testsuite/ld-scripts/output-section-types.t
5044c7
@@ -2,6 +2,13 @@ SECTIONS {
5044c7
   .rom  (NOLOAD)   : { LONG(1234); }
5044c7
   .ro   (READONLY) : { LONG(5678); }
5044c7
   .over (OVERLAY)  : { LONG(0123); }
5044c7
+  progbits (TYPE=SHT_PROGBITS) : { BYTE(1) }
5044c7
+  strtab (TYPE = SHT_STRTAB) : { BYTE(0) }
5044c7
+  note (TYPE =SHT_NOTE) : { BYTE(8) }
5044c7
+  init_array (TYPE= 14) : { QUAD(14) }
5044c7
+  fini_array ( TYPE=SHT_FINI_ARRAY) : { QUAD(15) }
5044c7
+  preinit_array (TYPE=SHT_PREINIT_ARRAY ) : { QUAD(16) }
5044c7
+  .ro.note   (READONLY (TYPE=SHT_NOTE)) : { LONG(5678); }
5044c7
   /DISCARD/        : { *(*) }
5044c7
 
5044c7
 }