Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -106,6 +106,12 @@ // Holds file name and line number for error reporting. std::string Location; + + // Keeps string representing the expression. Used when printing map file. + StringRef StringView; + + unsigned Offset; + unsigned Size; }; // Linker scripts allow additional constraints to be put on ouput sections. @@ -183,6 +189,9 @@ static bool classof(const BaseCommand *C) { return C->Kind == ByteKind; } + // Keeps string representing the expression. Used when printing map file. + StringRef StringView; + Expr Expression; unsigned Offset; unsigned Size; Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -731,7 +731,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; assignSymbol(Cmd, true); + Cmd->Size = Dot - Ctx->OutSec->Addr - Cmd->Offset; continue; } Index: ELF/MapFile.cpp =================================================================== --- ELF/MapFile.cpp +++ ELF/MapFile.cpp @@ -137,11 +137,29 @@ OS << OSec->Name << '\n'; // Dump symbols for each input section. - for (InputSection *IS : getInputSections(OSec)) { - writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(), IS->Alignment); - OS << indent(1) << toString(IS) << '\n'; - for (Symbol *Sym : SectionSyms[IS]) - OS << SymStr[Sym] << '\n'; + for (BaseCommand *Base : OSec->SectionCommands) { + if (auto *ISD = dyn_cast(Base)) { + for (InputSection *IS : ISD->Sections) { + writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(), + IS->Alignment); + OS << indent(1) << toString(IS) << '\n'; + for (Symbol *Sym : SectionSyms[IS]) + OS << SymStr[Sym] << '\n'; + } + continue; + } + + if (auto *Cmd = dyn_cast(Base)) { + writeHeader(OS, OSec->Addr + Cmd->Offset, Cmd->Size, Cmd->Size); + OS << indent(1) << Cmd->StringView << '\n'; + continue; + } + + if (auto *Cmd = dyn_cast(Base)) { + writeHeader(OS, OSec->Addr + Cmd->Offset, Cmd->Size, 1); + OS << indent(1) << Cmd->StringView << '\n'; + continue; + } } } } Index: ELF/ScriptParser.cpp =================================================================== --- ELF/ScriptParser.cpp +++ ELF/ScriptParser.cpp @@ -114,6 +114,9 @@ std::pair, std::vector> readSymbols(); + // Concatenates tokens in a [StartTok, EndTok] into string with Prefix. + StringRef buildStringView(StringRef Prefix, size_t StartTok, size_t EndTok); + // True if a script being read is in a subdirectory specified by -sysroot. bool IsUnderSysroot; @@ -773,7 +776,15 @@ return Cmd; } +StringRef ScriptParser::buildStringView(StringRef Prefix, size_t StartTok, size_t EndTok) { + std::string S = Prefix; + for (size_t I = StartTok; I <= EndTok; ++I) + S += (" " + Tokens[I]).str(); + return Saver.save(S); +} + SymbolAssignment *ScriptParser::readAssignment(StringRef Name) { + size_t OldPos = Pos; StringRef Op = next(); assert(Op == "=" || Op == "+="); Expr E = readExpr(); @@ -781,7 +792,10 @@ std::string Loc = getCurrentLocation(); E = [=] { return add(Script->getSymbolValue(Name, Loc), E()); }; } - return make(Name, E, getCurrentLocation()); + + SymbolAssignment *Ret = make(Name, E, getCurrentLocation()); + Ret->StringView = buildStringView(Name, OldPos, Pos - 1); + return Ret; } // This is an operator-precedence parser to parse a linker @@ -935,7 +949,11 @@ .Default(-1); if (Size == -1) return nullptr; - return make(readParenExpr(), Size); + + size_t OldPos = Pos; + ByteCommand* Ret = make(readParenExpr(), Size); + Ret->StringView = buildStringView(Tok, OldPos, Pos - 1); + return Ret; } StringRef ScriptParser::readParenLiteral() { Index: test/ELF/linkerscript/map-file.test =================================================================== --- test/ELF/linkerscript/map-file.test +++ test/ELF/linkerscript/map-file.test @@ -0,0 +1,38 @@ +# REQUIRES: x86 + +# RUN: echo ".section .foo.1,\"a\"" > %t.s +# RUN: echo ".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 +# RUN: FileCheck -strict-whitespace %s < %t.map + +SECTIONS { + . = 0x1000; + .foo : { + BYTE(0x11) + SHORT(0x1122) + LONG(0x11223344) + QUAD(0x1122334455667788) + . += 0x1000; + *(.foo.1) + . += 0x123 * + (1 + 1); + foo = .; + bar = 0x42 - 0x26; + } +} + +# CHECK: Address Size Align Out In Symbol +# CHECK-NEXT: 0000000000001000 000000000000125d 1 .foo +# CHECK-NEXT: 0000000000001000 0000000000000001 1 BYTE ( 0x11 ) +# CHECK-NEXT: 0000000000001001 0000000000000002 2 SHORT ( 0x1122 ) +# CHECK-NEXT: 0000000000001003 0000000000000004 4 LONG ( 0x11223344 ) +# CHECK-NEXT: 0000000000001007 0000000000000008 8 QUAD ( 0x1122334455667788 ) +# CHECK-NEXT: 000000000000100f 0000000000001000 1 . += 0x1000 +# CHECK-NEXT: 000000000000200f 0000000000000008 1 {{.*}}{{/|\\}}map-file.test.tmp.o:(.foo.1) +# 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)