Skip to content

Commit 4c3b297

Browse files
author
George Rimar
committedJan 28, 2019
[llvm-objdump] - Implement the --adjust-vma option.
GNU objdump's help says: "--adjust-vma: Add OFFSET to all displayed section addresses" In real life what it does is a bit more complicated (and IMO not always reasonable. For example, GNU objdump prints not only VMA, but also LMA for sections. And with --adjust-vma it adjusts LMA, but only when a section has relocations. llvm-objsump does not seem to support printing LMAs yet, but GNU's logic anyways does not make sense for me here). This patch tries to adjust VMA. I tried to implement a reasonable approach. I am not adjusting sections that are not allocatable. As, for example, adjusting debug sections VA's and rel[a] sections VA's should not make sense. This behavior seems to be GNU compatible. Differential revision: https://reviews.llvm.org/D57051 llvm-svn: 352347
1 parent 574e0c5 commit 4c3b297

File tree

2 files changed

+169
-4
lines changed

2 files changed

+169
-4
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# RUN: yaml2obj %s > %t
2+
# RUN: llvm-objdump --all-headers -D -z %t | FileCheck %s --check-prefixes=COMMON,NOADJUST
3+
# RUN: llvm-objdump --all-headers -D -z --adjust-vma=0x0 %t | FileCheck %s --check-prefixes=COMMON,NOADJUST
4+
# RUN: llvm-objdump --all-headers -D -z --adjust-vma=0x123000 %t | FileCheck %s --check-prefixes=COMMON,ADJUST
5+
6+
# NOADJUST: Disassembly of section .text:
7+
# NOADJUST-NEXT: 0000000000000000 sym:
8+
# NOADJUST-NEXT: 0: {{.*}} nop
9+
# NOADJUST: 0000000000000001 func:
10+
# NOADJUST-NEXT: 1: {{.*}} retq
11+
12+
# ADJUST: Disassembly of section .text:
13+
# ADJUST-NEXT: 0000000000123000 sym:
14+
# ADJUST-NEXT: 123000: {{.*}} nop
15+
# ADJUST: 0000000000123001 func:
16+
# ADJUST-NEXT: 123001: {{.*}} retq
17+
18+
# NOADJUST: Disassembly of section .debug_str:
19+
# NOADJUST-NEXT: 0000000000000000 .debug_str:
20+
# NOADJUST-NEXT: 0: {{.*}} %al, (%rax)
21+
# NOADJUST-NEXT: 0000000000000001: R_X86_64_32 .text
22+
# NOADJUST-NEXT: 2: {{.*}} addb %al, (%rax)
23+
24+
# ADJUST: Disassembly of section .debug_str:
25+
# ADJUST-NEXT: 0000000000000000 .debug_str:
26+
# ADJUST-NEXT: 0: {{.*}} %al, (%rax)
27+
# ADJUST-NEXT: 0000000000123001: R_X86_64_32 .text
28+
# ADJUST-NEXT: 2: {{.*}} addb %al, (%rax)
29+
30+
# COMMON: Disassembly of section .rela.debug_str:
31+
# COMMON-NEXT: 0000000000000000 .rela.debug_str:
32+
# COMMON-NEXT: 0: {{.*}} addl %eax, (%rax)
33+
## ... There are more lines here. We do not care.
34+
35+
# NOADJUST: Disassembly of section .data:
36+
# NOADJUST-NEXT: 0000000000000000 .data:
37+
# NOADJUST-NEXT: 0: {{.*}} addb %al, (%rax)
38+
# NOADJUST-NEXT: 0000000000000000: R_X86_64_32 .text
39+
# NOADJUST-NEXT: 2: {{.*}} addb %al, (%rax)
40+
41+
# ADJUST: Disassembly of section .data:
42+
# ADJUST-NEXT: 0000000000123000 .data:
43+
# ADJUST-NEXT: 123000: {{.*}} addb %al, (%rax)
44+
# ADJUST-NEXT: 0000000000123000: R_X86_64_32 .text
45+
# ADJUST-NEXT: 123002: {{.*}} addb %al, (%rax)
46+
47+
# COMMON: Disassembly of section .rela.data:
48+
# COMMON-NEXT: 0000000000000000 .rela.data:
49+
# COMMON-NEXT: 0: {{.*}} addb %al, (%rax)
50+
## ... There are more lines here. We do not care.
51+
52+
# NOADJUST: Sections:
53+
# NOADJUST-NEXT: Idx Name Size Address Type
54+
# NOADJUST-NEXT: 0 00000000 0000000000000000
55+
# NOADJUST-NEXT: 1 .text 00000002 0000000000000000 TEXT
56+
# NOADJUST-NEXT: 2 .debug_str 00000004 0000000000000000
57+
# NOADJUST-NEXT: 3 .rela.debug_str 00000018 0000000000000000
58+
# NOADJUST-NEXT: 4 .data 00000004 0000000000000000 DATA
59+
# NOADJUST-NEXT: 5 .rela.data 00000018 0000000000000000
60+
# NOADJUST-NEXT: 6 .symtab 00000060 0000000000000000
61+
# NOADJUST-NEXT: 7 .strtab 00000010 0000000000000000
62+
# NOADJUST-NEXT: 8 .shstrtab 0000003c 0000000000000000
63+
64+
# ADJUST: Sections:
65+
# ADJUST-NEXT: Idx Name Size Address Type
66+
# ADJUST-NEXT: 0 00000000 0000000000000000
67+
# ADJUST-NEXT: 1 .text 00000002 0000000000123000 TEXT
68+
# ADJUST-NEXT: 2 .debug_str 00000004 0000000000000000
69+
# ADJUST-NEXT: 3 .rela.debug_str 00000018 0000000000000000
70+
# ADJUST-NEXT: 4 .data 00000004 0000000000123000 DATA
71+
# ADJUST-NEXT: 5 .rela.data 00000018 0000000000000000
72+
# ADJUST-NEXT: 6 .symtab 00000060 0000000000000000
73+
# ADJUST-NEXT: 7 .strtab 00000010 0000000000000000
74+
# ADJUST-NEXT: 8 .shstrtab 0000003c 0000000000000000
75+
76+
# COMMON: SYMBOL TABLE:
77+
# COMMON-NEXT: 0000000000000001 l F .text 00000000 func
78+
# COMMON-NEXT: 0000000000000000 .text 00000000 sym
79+
# COMMON-NEXT: 0000000000000000 l d .text 00000000 .text
80+
81+
--- !ELF
82+
FileHeader:
83+
Class: ELFCLASS64
84+
Data: ELFDATA2LSB
85+
Type: ET_REL
86+
Machine: EM_X86_64
87+
Sections:
88+
- Name: .text
89+
Type: SHT_PROGBITS
90+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
91+
AddressAlign: 0x0000000000000004
92+
Content: 90C3
93+
- Name: .debug_str
94+
Type: SHT_PROGBITS
95+
Flags: [ SHF_MERGE, SHF_STRINGS ]
96+
AddressAlign: 0x0000000000000001
97+
Content: '00000000'
98+
- Name: .rela.debug_str
99+
Type: SHT_RELA
100+
Link: .symtab
101+
AddressAlign: 0x0000000000000008
102+
Info: .debug_str
103+
Relocations:
104+
- Offset: 0x0000000000000001
105+
Symbol: .text
106+
Type: R_X86_64_32
107+
- Name: .data
108+
Type: SHT_PROGBITS
109+
Flags: [ SHF_WRITE, SHF_ALLOC ]
110+
AddressAlign: 0x0000000000000001
111+
Content: '00000000'
112+
- Name: .rela.data
113+
Type: SHT_RELA
114+
Link: .symtab
115+
AddressAlign: 0x0000000000000008
116+
Info: .data
117+
Relocations:
118+
- Offset: 0x0000000000000000
119+
Symbol: .text
120+
Type: R_X86_64_32
121+
Symbols:
122+
Local:
123+
- Name: func
124+
Type: STT_FUNC
125+
Section: .text
126+
Value: 0x0000000000000001
127+
- Name: sym
128+
Section: .text
129+
- Name: .text
130+
Type: STT_SECTION
131+
Section: .text

‎llvm/tools/llvm-objdump/llvm-objdump.cpp

+38-4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
using namespace llvm;
7171
using namespace object;
7272

73+
cl::opt<unsigned long long> AdjustVMA(
74+
"adjust-vma",
75+
cl::desc("Increase the displayed address by the specified offset"),
76+
cl::value_desc("offset"), cl::init(0));
77+
7378
cl::opt<bool>
7479
llvm::AllHeaders("all-headers",
7580
cl::desc("Display all available header information"));
@@ -890,6 +895,18 @@ getRelocsMap(llvm::object::ObjectFile const &Obj) {
890895
return Ret;
891896
}
892897

898+
// Used for --adjust-vma to check if address should be adjusted by the
899+
// specified value for a given section.
900+
// For ELF we do not adjust non-allocatable sections like debug ones,
901+
// because they are not loadable.
902+
// TODO: implement for other file formats.
903+
static bool shouldAdjustVA(const SectionRef &Section) {
904+
const ObjectFile *Obj = Section.getObject();
905+
if (isa<object::ELFObjectFileBase>(Obj))
906+
return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC;
907+
return false;
908+
}
909+
893910
static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
894911
MCContext &Ctx, MCDisassembler *DisAsm,
895912
const MCInstrAnalysis *MIA, MCInstPrinter *IP,
@@ -1046,6 +1063,10 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
10461063
ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
10471064
BytesStr.size());
10481065

1066+
uint64_t VMAAdjustment = 0;
1067+
if (shouldAdjustVA(Section))
1068+
VMAAdjustment = AdjustVMA;
1069+
10491070
uint64_t Size;
10501071
uint64_t Index;
10511072
bool PrintedSection = false;
@@ -1110,7 +1131,8 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
11101131

11111132
outs() << '\n';
11121133
if (!NoLeadingAddr)
1113-
outs() << format("%016" PRIx64 " ", SectionAddr + Start);
1134+
outs() << format("%016" PRIx64 " ",
1135+
SectionAddr + Start + VMAAdjustment);
11141136

11151137
StringRef SymbolName = std::get<1>(Symbols[SI]);
11161138
if (Demangle)
@@ -1271,9 +1293,9 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
12711293
if (Size == 0)
12721294
Size = 1;
12731295

1274-
PIP.printInst(*IP, Disassembled ? &Inst : nullptr,
1275-
Bytes.slice(Index, Size), SectionAddr + Index, outs(), "",
1276-
*STI, &SP, &Rels);
1296+
PIP.printInst(
1297+
*IP, Disassembled ? &Inst : nullptr, Bytes.slice(Index, Size),
1298+
SectionAddr + Index + VMAAdjustment, outs(), "", *STI, &SP, &Rels);
12771299
outs() << CommentStream.str();
12781300
Comments.clear();
12791301

@@ -1353,6 +1375,15 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
13531375
if (Offset >= Index + Size)
13541376
break;
13551377

1378+
// When --adjust-vma is used, update the address printed.
1379+
if (RelCur->getSymbol() != Obj->symbol_end()) {
1380+
Expected<section_iterator> SymSI =
1381+
RelCur->getSymbol()->getSection();
1382+
if (SymSI && *SymSI != Obj->section_end() &&
1383+
(shouldAdjustVA(**SymSI)))
1384+
Offset += AdjustVMA;
1385+
}
1386+
13561387
printRelocation(*RelCur, SectionAddr + Offset,
13571388
Obj->getBytesInAddress());
13581389
++RelCur;
@@ -1493,6 +1524,9 @@ void llvm::printSectionHeaders(const ObjectFile *Obj) {
14931524
StringRef Name;
14941525
error(Section.getName(Name));
14951526
uint64_t Address = Section.getAddress();
1527+
if (shouldAdjustVA(Section))
1528+
Address += AdjustVMA;
1529+
14961530
uint64_t Size = Section.getSize();
14971531
bool Text = Section.isText();
14981532
bool Data = Section.isData();

0 commit comments

Comments
 (0)
Please sign in to comment.