diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -97,7 +97,7 @@ std::string memoryRegionName; std::string lmaRegionName; bool nonAlloc = false; - bool noload = false; + bool typeIsSet = false; bool expressionsUseSymbols = false; bool usedInExpression = false; bool inOverlay = false; diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -115,7 +115,8 @@ // If IS is the first section to be added to this section, // initialize type, entsize and flags from isec. hasInputSections = true; - type = isec->type; + if (!typeIsSet) + type = isec->type; entsize = isec->entsize; flags = isec->flags; } else { @@ -135,8 +136,6 @@ type = SHT_PROGBITS; } } - if (noload) - type = SHT_NOBITS; isec->parent = this; uint64_t andMask = @@ -451,14 +450,14 @@ writeInt(buf + data->offset, data->expression().getValue(), data->size); } -static void finalizeShtGroup(OutputSection *os, - InputSection *section) { - assert(config->relocatable); - +static void finalizeShtGroup(OutputSection *os, InputSection *section) { // sh_link field for SHT_GROUP sections should contain the section index of // the symbol table. os->link = in.symTab->getParent()->sectionIndex; + if (!section) + return; + // sh_info then contain index of an entry in symbol table section which // provides signature of the section group. ArrayRef symbols = section->file->getSymbols(); diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp --- a/lld/ELF/ScriptParser.cpp +++ b/lld/ELF/ScriptParser.cpp @@ -787,19 +787,41 @@ }; } +#define ECase(X) \ + { #X, X } +constexpr std::pair typeMap[] = { + ECase(SHT_PROGBITS), ECase(SHT_STRTAB), ECase(SHT_NOTE), + ECase(SHT_NOBITS), ECase(SHT_INIT_ARRAY), ECase(SHT_FINI_ARRAY), + ECase(SHT_PREINIT_ARRAY), ECase(SHT_GROUP), +}; + // Tries to read the special directive for an output section definition which // can be one of following: "(NOLOAD)", "(COPY)", "(INFO)" or "(OVERLAY)". // Tok1 and Tok2 are next 2 tokens peeked. See comment for readSectionAddressType below. bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok1, StringRef tok2) { if (tok1 != "(") return false; - if (tok2 != "NOLOAD" && tok2 != "COPY" && tok2 != "INFO" && tok2 != "OVERLAY") + if (tok2 != "NOLOAD" && tok2 != "COPY" && tok2 != "INFO" && + tok2 != "OVERLAY" && tok2 != "TYPE") return false; expect("("); if (consume("NOLOAD")) { - cmd->noload = true; cmd->type = SHT_NOBITS; + cmd->typeIsSet = true; + } else if (consume("TYPE")) { + expect("="); + StringRef value = peek(); + auto it = llvm::find_if(typeMap, [=](auto e) { return e.first == value; }); + if (it != std::end(typeMap)) { + // The value is a recognized literal SHT_*. + cmd->type = it->second; + skip(); + } else { + // Otherwise, read an expression. + cmd->type = readExpr()().getValue(); + } + cmd->typeIsSet = true; } else { skip(); // This is "COPY", "INFO" or "OVERLAY". cmd->nonAlloc = true; @@ -820,7 +842,11 @@ // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html // https://sourceware.org/binutils/docs/ld/Output-Section-Type.html void ScriptParser::readSectionAddressType(OutputSection *cmd) { - if (readSectionDirective(cmd, peek(), peek2())) + // Temporarily set inExpr to support TYPE= without spaces. + bool saved = std::exchange(inExpr, true); + bool isDirective = readSectionDirective(cmd, peek(), peek2()); + inExpr = saved; + if (isDirective) return; cmd->addrExpr = readExpr(); diff --git a/lld/docs/ELF/linker_script.rst b/lld/docs/ELF/linker_script.rst --- a/lld/docs/ELF/linker_script.rst +++ b/lld/docs/ELF/linker_script.rst @@ -94,6 +94,17 @@ GNU ld from Binutils 2.35 onwards will reduce sh_addralign so that sh_addr=0 (modulo sh_addralign). +Output section type +------------------- + +When an *OutputSection* *S* has ``(type)``, LLD will set sh_type or sh_flags of *S*. + +- ``NOLOAD``: set ``sh_type`` to ``SHT_NOBITS``. +- ``COPY``, ``INFO``, ``OVERLAY``: clear the ``SHF_ALLOC`` bit in ``sh_flags``. +- ``TYPE=``: set ``sh_type`` to the specified value. ```` must be + an integer or one of ``SHT_PROGBITS, SHT_STRTAB, SHT_NOTE, SHT_NOBITS, + SHT_INIT_ARRAY, SHT_FINI_ARRAY, SHT_PREINIT_ARRAY, SHT_GROUP``. + Output section alignment ------------------------ diff --git a/lld/test/ELF/linkerscript/custom-section-type.s b/lld/test/ELF/linkerscript/custom-section-type.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/linkerscript/custom-section-type.s @@ -0,0 +1,60 @@ +# REQUIRES: x86 +## TYPE= customizes the output section type. + +# RUN: rm -rf %t && split-file %s %t +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %t/a.s -o %t/a.o +# RUN: ld.lld -T %t/t %t/a.o -o %t/a +# RUN: llvm-readelf -S %t/a | FileCheck %s + +# RUN: ld.lld -r -T %t/t %t/a.o -o %t/a.ro +# RUN: llvm-readelf -S %t/a.ro | FileCheck %s + +# CHECK: [Nr] Name Type Address Off Size ES Flg Lk Inf Al +# CHECK-NEXT: [ 0] NULL [[#%x,]] [[#%x,]] 000000 00 0 0 0 +# CHECK-NEXT: [ 1] progbits PROGBITS [[#%x,]] [[#%x,]] 000001 00 A 0 0 1 +# CHECK-NEXT: [ 2] note NOTE [[#%x,]] [[#%x,]] 000002 00 A 0 0 1 +# CHECK-NEXT: [ 3] nobits NOBITS [[#%x,]] [[#%x,]] 000001 00 A 0 0 1 +# CHECK-NEXT: [ 4] init_array INIT_ARRAY [[#%x,]] [[#%x,]] 000008 00 A 0 0 1 +# CHECK-NEXT: [ 5] fini_array FINI_ARRAY [[#%x,]] [[#%x,]] 000008 00 A 0 0 1 +# CHECK-NEXT: [ 6] preinit_array PREINIT_ARRAY [[#%x,]] [[#%x,]] 000008 00 A 0 0 1 +# CHECK-NEXT: [ 7] group GROUP [[#%x,]] [[#%x,]] 000004 00 A [[#SYMTAB:]] 0 1 +# CHECK-NEXT: [ 8] expr 0x42: [[#%x,]] [[#%x,]] 000002 00 A 0 0 1 + +# CHECK: [[[#SYMTAB]]] .symtab SYMTAB + +# RUN: not ld.lld -T %t/t.err1 %t/a.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR1 +# ERR1: error: {{.*}}.err1:1: = expected, but got ) + +# RUN: not ld.lld -T %t/t.err2 %t/a.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR2 +# ERR2: error: {{.*}}.err2:1: symbol not found: SHT_HASH + +#--- a.s +.globl _start +_start: + +## TYPE = SHT_NOTE below overrides the input section type. +.section note,"a",@progbits +.byte 0 + +## TYPE=0x42 below overrides the input section type. +## Do we need a warning for a non-canMergeToProgbits type? +.section expr,"a",@12345 +.byte 0 + +#--- t +SECTIONS { + progbits (TYPE=SHT_PROGBITS) : { BYTE(1) } + note (TYPE = SHT_NOTE) : { BYTE(7) } + nobits ( TYPE=SHT_NOBITS) : { BYTE(8) } + init_array (TYPE=SHT_INIT_ARRAY ) : { QUAD(14) } + fini_array (TYPE=SHT_FINI_ARRAY) : { QUAD(15) } + preinit_array (TYPE=SHT_PREINIT_ARRAY) : { QUAD(16) } + group (TYPE=SHT_GROUP) : { LONG(17) } + expr (TYPE=0x41+1) : { BYTE(0x42) } +} + +#--- t.err1 +SECTIONS { err (TYPE) : {} } + +#--- t.err2 +SECTIONS { err (TYPE=SHT_HASH) : {} }