Index: ELF/MapFile.cpp =================================================================== --- ELF/MapFile.cpp +++ ELF/MapFile.cpp @@ -42,12 +42,13 @@ static const std::string Indent8 = " "; // 8 spaces static const std::string Indent16 = " "; // 16 spaces +static int AddrWidth = 0; +static int SizeWidth = 0; // Print out the first three columns of a line. static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size, uint64_t Align) { - int W = Config->Is64 ? 16 : 8; - OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align); + OS << format("%*llx %*llx %5lld ", AddrWidth, Addr, SizeWidth, Size, Align); } // Returns a list of all symbols that we want to print out. @@ -149,6 +150,22 @@ } } +static int getFieldWidth(uint64_t Val) { + unsigned HexSize = Val ? (llvm::Log2_64(Val) / llvm::Log2_64(16) + 1) : 1; + return std::max(HexSize + 1 /* Padding */, 6 /* MinWidth */); +} + +static void setColumnsWidth() { + uint64_t MaxAddr = 0; + uint64_t MaxSize = 0; + for (OutputSection *Sec : OutputSections) { + MaxAddr = std::max(MaxAddr, std::max(Sec->getLMA(), Sec->Addr)); + MaxSize = std::max(MaxSize, Sec->Size); + } + AddrWidth = getFieldWidth(MaxAddr); + SizeWidth = getFieldWidth(MaxSize); +} + void elf::writeMapFile() { if (Config->MapFile.empty()) return; @@ -161,15 +178,16 @@ return; } + setColumnsWidth(); + // Collect symbol info that we want to print out. std::vector Syms = getSymbols(); SymbolMapTy SectionSyms = getSectionSyms(Syms); DenseMap SymStr = getSymbolStrings(Syms); // Print out the header line. - int W = Config->Is64 ? 16 : 8; - OS << left_justify("Address", W) << ' ' << left_justify("Size", W) - << " Align Out In Symbol\n"; + OS << right_justify("VMA", AddrWidth) << ' ' + << right_justify("Size", SizeWidth) << " Align Out In Symbol\n"; // Print out file contents. for (OutputSection *OSec : OutputSections) { Index: test/ELF/linkerscript/map-file.test =================================================================== --- test/ELF/linkerscript/map-file.test +++ test/ELF/linkerscript/map-file.test @@ -22,16 +22,16 @@ } } -# CHECK: Address Size Align Out In Symbol -# 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 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) +# CHECK: VMA Size Align Out In Symbol +# CHECK-NEXT: 1000 125d 1 .foo +# CHECK-NEXT: 1000 1 1 BYTE ( 0x11 ) +# CHECK-NEXT: 1001 2 1 SHORT ( 0x1122 ) +# CHECK-NEXT: 1003 4 1 LONG ( 0x11223344 ) +# CHECK-NEXT: 1007 8 1 QUAD ( 0x1122334455667788 ) +# CHECK-NEXT: 100f 1000 1 . += 0x1000 +# CHECK-NEXT: 200f 8 1 {{.*}}{{/|\\}}map-file.test.tmp.o:(.foo.1) +# CHECK-NEXT: 2017 246 1 . += 0x123 * ( 1 + 1 ) +# CHECK-NEXT: 225d 0 1 foo = . +# CHECK-NEXT: 225d 0 1 bar = 0x42 - 0x26 +# CHECK-NEXT: 2260 0 4 .text +# CHECK-NEXT: 2260 0 4 {{.*}}{{/|\\}}map-file.test.tmp.o:(.text) Index: test/ELF/map-file.s =================================================================== --- test/ELF/map-file.s +++ test/ELF/map-file.s @@ -40,60 +40,60 @@ abs = 0xAB5 labs = 0x1AB5 -// CHECK: Address Size Align Out In Symbol -// CHECK-NEXT: 00000000002001c8 0000000000000078 8 .dynsym -// CHECK-NEXT: 00000000002001c8 0000000000000078 8 :(.dynsym) -// CHECK-NEXT: 0000000000200240 000000000000002c 8 .gnu.hash -// CHECK-NEXT: 0000000000200240 000000000000002c 8 :(.gnu.hash) -// CHECK-NEXT: 000000000020026c 0000000000000030 4 .hash -// CHECK-NEXT: 000000000020026c 0000000000000030 4 :(.hash) -// CHECK-NEXT: 000000000020029c 0000000000000031 1 .dynstr -// CHECK-NEXT: 000000000020029c 0000000000000031 1 :(.dynstr) -// CHECK-NEXT: 00000000002002d0 0000000000000030 8 .rela.dyn -// CHECK-NEXT: 00000000002002d0 0000000000000030 8 :(.rela.dyn) -// CHECK-NEXT: 0000000000200300 0000000000000030 8 .rela.plt -// CHECK-NEXT: 0000000000200300 0000000000000030 8 :(.rela.plt) -// CHECK-NEXT: 0000000000200330 0000000000000060 8 .eh_frame -// CHECK-NEXT: 0000000000200330 000000000000002c 0 {{.*}}{{/|\\}}map-file.s.tmp1.o:(.eh_frame+0x0) -// CHECK-NEXT: 0000000000200360 0000000000000014 0 {{.*}}{{/|\\}}map-file.s.tmp1.o:(.eh_frame+0x2c) -// CHECK-NEXT: 0000000000200378 0000000000000018 0 {{.*}}{{/|\\}}map-file.s.tmp2.o:(.eh_frame+0x18) -// CHECK-NEXT: 0000000000201000 000000000000002d 4 .text -// CHECK-NEXT: 0000000000201000 0000000000000028 4 {{.*}}{{/|\\}}map-file.s.tmp1.o:(.text) -// CHECK-NEXT: 0000000000201000 0000000000000000 0 _start -// CHECK-NEXT: 000000000020101f 0000000000000000 0 f(int) -// CHECK-NEXT: 0000000000201028 0000000000000000 0 local -// CHECK-NEXT: 0000000000201028 0000000000000002 4 {{.*}}{{/|\\}}map-file.s.tmp2.o:(.text) -// CHECK-NEXT: 0000000000201028 0000000000000000 0 foo -// CHECK-NEXT: 0000000000201029 0000000000000000 0 bar -// CHECK-NEXT: 000000000020102a 0000000000000000 1 {{.*}}{{/|\\}}map-file.s.tmp2.o:(.text.zed) -// CHECK-NEXT: 000000000020102a 0000000000000000 0 zed -// CHECK-NEXT: 000000000020102c 0000000000000000 4 {{.*}}{{/|\\}}map-file.s.tmp3.o:(.text) -// CHECK-NEXT: 000000000020102c 0000000000000000 0 bah -// CHECK-NEXT: 000000000020102c 0000000000000001 4 {{.*}}{{/|\\}}map-file.s.tmp4.a(map-file.s.tmp4.o):(.text) -// CHECK-NEXT: 000000000020102c 0000000000000000 0 baz -// CHECK-NEXT: 0000000000201030 0000000000000030 16 .plt -// CHECK-NEXT: 0000000000201030 0000000000000030 16 :(.plt) -// CHECK-NEXT: 0000000000201040 0000000000000000 0 sharedFunc1 -// CHECK-NEXT: 0000000000201050 0000000000000000 0 sharedFunc2 -// CHECK-NEXT: 0000000000202000 0000000000000028 8 .got.plt -// CHECK-NEXT: 0000000000202000 0000000000000028 8 :(.got.plt) -// CHECK-NEXT: 0000000000203000 0000000000000100 8 .dynamic -// CHECK-NEXT: 0000000000203000 0000000000000100 8 :(.dynamic) -// CHECK-NEXT: 0000000000204000 0000000000000010 16 .bss -// CHECK-NEXT: 0000000000204000 0000000000000004 16 {{.*}}{{/|\\}}map-file.s.tmp1.o:(COMMON) -// CHECK-NEXT: 0000000000204000 0000000000000004 0 common -// CHECK-NEXT: 0000000000204004 0000000000000004 1 :(.bss) -// CHECK-NEXT: 0000000000204004 0000000000000004 0 sharedFoo -// CHECK-NEXT: 0000000000204008 0000000000000008 1 :(.bss) -// CHECK-NEXT: 0000000000204008 0000000000000008 0 sharedBar -// CHECK-NEXT: 0000000000000000 0000000000000008 1 .comment -// CHECK-NEXT: 0000000000000000 0000000000000008 1 :(.comment) -// CHECK-NEXT: 0000000000000000 0000000000000198 8 .symtab -// CHECK-NEXT: 0000000000000000 0000000000000198 8 :(.symtab) -// CHECK-NEXT: 0000000000000000 0000000000000084 1 .shstrtab -// CHECK-NEXT: 0000000000000000 0000000000000084 1 :(.shstrtab) -// CHECK-NEXT: 0000000000000000 000000000000006d 1 .strtab -// CHECK-NEXT: 0000000000000000 000000000000006d 1 :(.strtab) +// CHECK: VMA Size Align Out In Symbol +// CHECK-NEXT: 2001c8 78 8 .dynsym +// CHECK-NEXT: 2001c8 78 8 :(.dynsym) +// CHECK-NEXT: 200240 2c 8 .gnu.hash +// CHECK-NEXT: 200240 2c 8 :(.gnu.hash) +// CHECK-NEXT: 20026c 30 4 .hash +// CHECK-NEXT: 20026c 30 4 :(.hash) +// CHECK-NEXT: 20029c 31 1 .dynstr +// CHECK-NEXT: 20029c 31 1 :(.dynstr) +// CHECK-NEXT: 2002d0 30 8 .rela.dyn +// CHECK-NEXT: 2002d0 30 8 :(.rela.dyn) +// CHECK-NEXT: 200300 30 8 .rela.plt +// CHECK-NEXT: 200300 30 8 :(.rela.plt) +// CHECK-NEXT: 200330 60 8 .eh_frame +// CHECK-NEXT: 200330 2c 0 {{.*}}{{/|\\}}map-file.s.tmp1.o:(.eh_frame+0x0) +// CHECK-NEXT: 200360 14 0 {{.*}}{{/|\\}}map-file.s.tmp1.o:(.eh_frame+0x2c) +// CHECK-NEXT: 200378 18 0 {{.*}}{{/|\\}}map-file.s.tmp2.o:(.eh_frame+0x18) +// CHECK-NEXT: 201000 2d 4 .text +// CHECK-NEXT: 201000 28 4 {{.*}}{{/|\\}}map-file.s.tmp1.o:(.text) +// CHECK-NEXT: 201000 0 0 _start +// CHECK-NEXT: 20101f 0 0 f(int) +// CHECK-NEXT: 201028 0 0 local +// CHECK-NEXT: 201028 2 4 {{.*}}{{/|\\}}map-file.s.tmp2.o:(.text) +// CHECK-NEXT: 201028 0 0 foo +// CHECK-NEXT: 201029 0 0 bar +// CHECK-NEXT: 20102a 0 1 {{.*}}{{/|\\}}map-file.s.tmp2.o:(.text.zed) +// CHECK-NEXT: 20102a 0 0 zed +// CHECK-NEXT: 20102c 0 4 {{.*}}{{/|\\}}map-file.s.tmp3.o:(.text) +// CHECK-NEXT: 20102c 0 0 bah +// CHECK-NEXT: 20102c 1 4 {{.*}}{{/|\\}}map-file.s.tmp4.a(map-file.s.tmp4.o):(.text) +// CHECK-NEXT: 20102c 0 0 baz +// CHECK-NEXT: 201030 30 16 .plt +// CHECK-NEXT: 201030 30 16 :(.plt) +// CHECK-NEXT: 201040 0 0 sharedFunc1 +// CHECK-NEXT: 201050 0 0 sharedFunc2 +// CHECK-NEXT: 202000 28 8 .got.plt +// CHECK-NEXT: 202000 28 8 :(.got.plt) +// CHECK-NEXT: 203000 100 8 .dynamic +// CHECK-NEXT: 203000 100 8 :(.dynamic) +// CHECK-NEXT: 204000 10 16 .bss +// CHECK-NEXT: 204000 4 16 {{.*}}{{/|\\}}map-file.s.tmp1.o:(COMMON) +// CHECK-NEXT: 204000 4 0 common +// CHECK-NEXT: 204004 4 1 :(.bss) +// CHECK-NEXT: 204004 4 0 sharedFoo +// CHECK-NEXT: 204008 8 1 :(.bss) +// CHECK-NEXT: 204008 8 0 sharedBar +// CHECK-NEXT: 0 8 1 .comment +// CHECK-NEXT: 0 8 1 :(.comment) +// CHECK-NEXT: 0 198 8 .symtab +// CHECK-NEXT: 0 198 8 :(.symtab) +// CHECK-NEXT: 0 84 1 .shstrtab +// CHECK-NEXT: 0 84 1 :(.shstrtab) +// CHECK-NEXT: 0 6d 1 .strtab +// CHECK-NEXT: 0 6d 1 :(.strtab) // RUN: not ld.lld %t1.o %t2.o %t3.o %t4.a -o %t -Map=/ 2>&1 \ // RUN: | FileCheck -check-prefix=FAIL %s