Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -86,10 +86,8 @@ // This represents ". = " or " = ". struct SymbolAssignment : BaseCommand { - SymbolAssignment(StringRef Name, Expr E, std::string Loc, - std::string CommandString) - : BaseCommand(AssignmentKind), Name(Name), Expression(E), Location(Loc), - CommandString(CommandString) {} + SymbolAssignment(StringRef Name, Expr E, std::string Loc) + : BaseCommand(AssignmentKind), Name(Name), Expression(E), Location(Loc) {} static bool classof(const BaseCommand *C) { return C->Kind == AssignmentKind; @@ -112,11 +110,11 @@ // A string representation of this command. We use this for -Map. std::string CommandString; - // This is just an offset of this assignment command in the output section. - unsigned Offset; + // Address of this assignment command. + unsigned Addr; - // Size of this assignment command. This is usually 0, but if you move '.' - // or use a BYTE()-family command, this may be greater than 0." + // Size of this assignment command. This is usually 0, but if + // you move '.' this may be greater than 0. unsigned Size; }; @@ -200,7 +198,11 @@ std::string CommandString; Expr Expression; + + // This is just an offset of this assignment command in the output section. unsigned Offset; + + // Size of this data command. unsigned Size; }; Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -788,9 +788,9 @@ for (BaseCommand *Base : Sec->SectionCommands) { // This handles the assignments to symbol or to the dot. if (auto *Cmd = dyn_cast(Base)) { - Cmd->Offset = Dot - Ctx->OutSec->Addr; + Cmd->Addr = Dot; assignSymbol(Cmd, true); - Cmd->Size = Dot - Ctx->OutSec->Addr - Cmd->Offset; + Cmd->Size = Dot - Cmd->Addr; continue; } @@ -1049,7 +1049,9 @@ for (BaseCommand *Base : SectionCommands) { if (auto *Cmd = dyn_cast(Base)) { + Cmd->Addr = Dot; assignSymbol(Cmd, false); + Cmd->Size = Dot - Cmd->Addr; continue; } Index: ELF/MapFile.cpp =================================================================== --- ELF/MapFile.cpp +++ ELF/MapFile.cpp @@ -171,8 +171,19 @@ OS << left_justify("Address", W) << ' ' << left_justify("Size", W) << " Align Out In Symbol\n"; - // Print out file contents. - for (OutputSection *OSec : OutputSections) { + for (BaseCommand *Base : Script->SectionCommands) { + if (auto *Cmd = dyn_cast(Base)) { + if (Cmd->Provide && !Cmd->Sym) + continue; + writeHeader(OS, Cmd->Addr, Cmd->Size, 1); + OS << Cmd->CommandString << '\n'; + continue; + } + + auto *OSec = dyn_cast(Base); + if (!OSec) + continue; + writeHeader(OS, OSec->Addr, OSec->Size, OSec->Alignment); OS << OSec->Name << '\n'; @@ -200,7 +211,9 @@ } if (auto *Cmd = dyn_cast(Base)) { - writeHeader(OS, OSec->Addr + Cmd->Offset, Cmd->Size, 1); + if (Cmd->Provide && !Cmd->Sym) + continue; + writeHeader(OS, Cmd->Addr, Cmd->Size, 1); OS << Indent8 << Cmd->CommandString << '\n'; continue; } Index: ELF/ScriptParser.cpp =================================================================== --- ELF/ScriptParser.cpp +++ ELF/ScriptParser.cpp @@ -267,8 +267,7 @@ Expr E = readExpr(); if (!atEOF()) setError("EOF expected, but got " + next()); - SymbolAssignment *Cmd = make(Name, E, getCurrentLocation(), - "" /*CommandString*/); + SymbolAssignment *Cmd = make(Name, E, getCurrentLocation()); Script->SectionCommands.push_back(Cmd); } @@ -773,27 +772,31 @@ Cmd->Provide = Provide; Cmd->Hidden = Hidden; expect(")"); - expect(";"); return Cmd; } SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) { + size_t OldPos = Pos; SymbolAssignment *Cmd = nullptr; - if (peek() == "=" || peek() == "+=") { + if (peek() == "=" || peek() == "+=") Cmd = readAssignment(Tok); - expect(";"); - } else if (Tok == "PROVIDE") { + else if (Tok == "PROVIDE") Cmd = readProvideHidden(true, false); - } else if (Tok == "HIDDEN") { + else if (Tok == "HIDDEN") Cmd = readProvideHidden(false, true); - } else if (Tok == "PROVIDE_HIDDEN") { + else if (Tok == "PROVIDE_HIDDEN") Cmd = readProvideHidden(true, true); + + if (Cmd) { + Cmd->CommandString = + Tok.str() + " " + + llvm::join(Tokens.begin() + OldPos, Tokens.begin() + Pos, " "); + expect(";"); } return Cmd; } SymbolAssignment *ScriptParser::readAssignment(StringRef Name) { - size_t OldPos = Pos; StringRef Op = next(); assert(Op == "=" || Op == "+="); Expr E = readExpr(); @@ -801,11 +804,7 @@ std::string Loc = getCurrentLocation(); E = [=] { return add(Script->getSymbolValue(Name, Loc), E()); }; } - - std::string CommandString = - Name.str() + " " + - llvm::join(Tokens.begin() + OldPos, Tokens.begin() + Pos, " "); - return make(Name, E, getCurrentLocation(), CommandString); + return make(Name, E, getCurrentLocation()); } // This is an operator-precedence parser to parse a linker Index: test/ELF/linkerscript/map-file.test =================================================================== --- test/ELF/linkerscript/map-file.test +++ test/ELF/linkerscript/map-file.test @@ -1,6 +1,6 @@ # REQUIRES: x86 -# RUN: echo '.section .foo.1, "a"; .quad 1' > %t.s +# RUN: echo '.quad sym3; .quad sym4; .section .foo.1, "a"; .quad 1' > %t.s # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %t.s -o %t.o # RUN: ld.lld -o %t %t.o -Map=%t.map --script %s @@ -13,25 +13,40 @@ SHORT(0x1122) LONG(0x11223344) QUAD(0x1122334455667788) + PROVIDE_HIDDEN(sym4 = .); . += 0x1000; *(.foo.1) + PROVIDE(unused1 = 0xff); + HIDDEN(sym6 = .); . += 0x123 * (1 + 1); foo = .; bar = 0x42 - 0x26; } + sym1 = .; + . += 0x500; + sym2 = .; + PROVIDE(unused2 = 0xff); + PROVIDE(sym3 = 42); } # CHECK: Address Size Align Out In Symbol +# CHECK-NEXT: 0000000000000000 0000000000001000 1 . = 0x1000 # CHECK-NEXT: 0000000000001000 000000000000125d 1 .foo # CHECK-NEXT: 0000000000001000 0000000000000001 1 BYTE ( 0x11 ) # CHECK-NEXT: 0000000000001001 0000000000000002 1 SHORT ( 0x1122 ) # CHECK-NEXT: 0000000000001003 0000000000000004 1 LONG ( 0x11223344 ) # CHECK-NEXT: 0000000000001007 0000000000000008 1 QUAD ( 0x1122334455667788 ) +# CHECK-NEXT: 000000000000100f 0000000000000000 1 PROVIDE_HIDDEN ( sym4 = . ) # CHECK-NEXT: 000000000000100f 0000000000001000 1 . += 0x1000 # CHECK-NEXT: 000000000000200f 0000000000000008 1 {{.*}}{{/|\\}}map-file.test.tmp.o:(.foo.1) +# CHECK-NEXT: 0000000000002017 0000000000000000 1 HIDDEN ( sym6 = . ) # CHECK-NEXT: 0000000000002017 0000000000000246 1 . += 0x123 * ( 1 + 1 ) # CHECK-NEXT: 000000000000225d 0000000000000000 1 foo = . # CHECK-NEXT: 000000000000225d 0000000000000000 1 bar = 0x42 - 0x26 -# CHECK-NEXT: 0000000000002260 0000000000000000 4 .text -# CHECK-NEXT: 0000000000002260 0000000000000000 4 {{.*}}{{/|\\}}map-file.test.tmp.o:(.text) +# CHECK-NEXT: 000000000000225d 0000000000000000 1 sym1 = . +# CHECK-NEXT: 000000000000225d 0000000000000500 1 . += 0x500 +# CHECK-NEXT: 000000000000275d 0000000000000000 1 sym2 = . +# CHECK-NEXT: 000000000000275d 0000000000000000 1 PROVIDE ( sym3 = 42 ) +# CHECK-NEXT: 0000000000002760 0000000000000010 4 .text +# CHECK-NEXT: 0000000000002760 0000000000000010 4 {{.*}}{{/|\\}}map-file.test.tmp.o:(.text)