Blame SOURCES/binutils-section-type.patch

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