Blame SOURCES/binutils-section-type.patch

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