Blame SOURCES/binutils-section-type.patch

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