Skip to content

Commit 0db3a5a

Browse files
author
Alexander Shaposhnikov
committed
[llvm-objcopy][MachO] Handle relocation entries where r_extern is zero
Fix handling of relocations with r_extern == 0. If r_extern == 0 then r_symbolnum is an index of a section rather than a symbol index. Patch by Seiya Nuta and Alexander Shaposhnikov. Test plan: make check-all Differential revision: https://reviews.llvm.org/D78946
1 parent bc03423 commit 0db3a5a

File tree

5 files changed

+130
-12
lines changed

5 files changed

+130
-12
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# RUN: yaml2obj %s -o %t
2+
3+
## Show that llvm-objcopy copies relocation entries where r_extern = 0.
4+
# RUN: llvm-objcopy %t %t2
5+
# RUN: cmp %t %t2
6+
7+
## Show that llvm-objcopy updates section indices properly.
8+
# RUN: llvm-objcopy --remove-section=__DATA,__foo %t %t3
9+
# RUN: llvm-objdump --macho --reloc %t3 | FileCheck %s
10+
11+
# CHECK: Relocation information (__DATA,__bar) 2 entries
12+
# CHECK-NEXT: address pcrel length extern type scattered symbolnum/value
13+
# CHECK-NEXT: 00000000 False quad False SUB False 2 (__DATA,__bar)
14+
# CHECK-NEXT: 00000000 False quad False UNSIGND False 1 (__TEXT,__text)
15+
16+
--- !mach-o
17+
FileHeader:
18+
magic: 0xFEEDFACF
19+
cputype: 0x01000007
20+
cpusubtype: 0x00000003
21+
filetype: 0x00000001
22+
ncmds: 1
23+
sizeofcmds: 312
24+
flags: 0x00000000
25+
reserved: 0x00000000
26+
LoadCommands:
27+
- cmd: LC_SEGMENT_64
28+
cmdsize: 312
29+
segname: ''
30+
vmaddr: 0
31+
vmsize: 24
32+
fileoff: 344
33+
filesize: 24
34+
maxprot: 7
35+
initprot: 7
36+
nsects: 3
37+
flags: 0
38+
Sections:
39+
- sectname: __text
40+
segname: __TEXT
41+
addr: 0x0000000000000000
42+
size: 8
43+
offset: 0x00000158
44+
align: 0
45+
reloff: 0x00000000
46+
nreloc: 0
47+
flags: 0x80000000
48+
reserved1: 0x00000000
49+
reserved2: 0x00000000
50+
reserved3: 0x00000000
51+
content: '0000000000000000'
52+
- sectname: __foo
53+
segname: __DATA
54+
addr: 0x0000000000000008
55+
size: 8
56+
offset: 0x00000160
57+
align: 0
58+
reloff: 0x00000000
59+
nreloc: 0
60+
flags: 0x00000000
61+
reserved1: 0x00000000
62+
reserved2: 0x00000000
63+
reserved3: 0x00000000
64+
content: '0000000000000000'
65+
- sectname: __bar
66+
segname: __DATA
67+
addr: 0x0000000000000010
68+
size: 8
69+
offset: 0x00000168
70+
align: 0
71+
reloff: 0x00000170
72+
nreloc: 2
73+
flags: 0x00000000
74+
reserved1: 0x00000000
75+
reserved2: 0x00000000
76+
reserved3: 0x00000000
77+
content: F0FFFFFFFFFFFFFF
78+
relocations:
79+
- address: 0x00000000
80+
symbolnum: 3
81+
pcrel: false
82+
length: 3
83+
extern: false
84+
type: 5
85+
scattered: false
86+
value: 0
87+
- address: 0x00000000
88+
symbolnum: 1
89+
pcrel: false
90+
length: 3
91+
extern: false
92+
type: 0
93+
scattered: false
94+
value: 0
95+
...

llvm/tools/llvm-objcopy/MachO/MachOReader.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd,
103103
R.Symbol = nullptr; // We'll fill this field later.
104104
R.Info = MachOObj.getRelocation(RI->getRawDataRefImpl());
105105
R.Scattered = MachOObj.isRelocationScattered(R.Info);
106+
R.Extern = !R.Scattered && MachOObj.getPlainRelocationExternal(R.Info);
106107
S.Relocations.push_back(R);
107108
}
108109

@@ -203,12 +204,27 @@ void MachOReader::readSymbolTable(Object &O) const {
203204
}
204205

205206
void MachOReader::setSymbolInRelocationInfo(Object &O) const {
207+
std::vector<const Section *> Sections;
208+
for (auto &LC : O.LoadCommands)
209+
for (std::unique_ptr<Section> &Sec : LC.Sections)
210+
Sections.push_back(Sec.get());
211+
206212
for (LoadCommand &LC : O.LoadCommands)
207213
for (std::unique_ptr<Section> &Sec : LC.Sections)
208214
for (auto &Reloc : Sec->Relocations)
209-
if (!Reloc.Scattered)
210-
Reloc.Symbol = O.SymTable.getSymbolByIndex(
211-
Reloc.getPlainRelocationSymbolNum(MachOObj.isLittleEndian()));
215+
if (!Reloc.Scattered) {
216+
const uint32_t SymbolNum =
217+
Reloc.getPlainRelocationSymbolNum(MachOObj.isLittleEndian());
218+
if (Reloc.Extern) {
219+
Reloc.Symbol = O.SymTable.getSymbolByIndex(SymbolNum);
220+
} else {
221+
// FIXME: Refactor error handling in MachOReader and report an error
222+
// if we encounter an invalid relocation.
223+
assert(SymbolNum >= 1 && SymbolNum <= Sections.size() &&
224+
"Invalid section index.");
225+
Reloc.Section = Sections[SymbolNum - 1];
226+
}
227+
}
212228
}
213229

214230
void MachOReader::readRebaseInfo(Object &O) const {

llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,13 @@ void MachOWriter::writeSections() {
239239
memcpy(B.getBufferStart() + Sec->Offset, Sec->Content.data(),
240240
Sec->Content.size());
241241
for (size_t Index = 0; Index < Sec->Relocations.size(); ++Index) {
242-
auto RelocInfo = Sec->Relocations[Index];
243-
if (!RelocInfo.Scattered)
244-
RelocInfo.setPlainRelocationSymbolNum(RelocInfo.Symbol->Index,
245-
IsLittleEndian);
246-
242+
RelocationInfo RelocInfo = Sec->Relocations[Index];
243+
if (!RelocInfo.Scattered) {
244+
const uint32_t SymbolNum = RelocInfo.Extern
245+
? (*RelocInfo.Symbol)->Index
246+
: (*RelocInfo.Section)->Index;
247+
RelocInfo.setPlainRelocationSymbolNum(SymbolNum, IsLittleEndian);
248+
}
247249
if (IsLittleEndian != sys::IsLittleEndianHost)
248250
MachO::swapStruct(
249251
reinterpret_cast<MachO::any_relocation_info &>(RelocInfo.Info));

llvm/tools/llvm-objcopy/MachO/Object.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ Error Object::removeSections(
6060
for (const LoadCommand &LC : LoadCommands)
6161
for (const std::unique_ptr<Section> &Sec : LC.Sections)
6262
for (const RelocationInfo &R : Sec->Relocations)
63-
if (R.Symbol && DeadSymbols.count(R.Symbol))
63+
if (R.Symbol && *R.Symbol && DeadSymbols.count(*R.Symbol))
6464
return createStringError(std::errc::invalid_argument,
6565
"symbol '%s' defined in section with index "
6666
"'%u' cannot be removed because it is "
6767
"referenced by a relocation in section '%s'",
68-
R.Symbol->Name.c_str(),
69-
*(R.Symbol->section()),
68+
(*R.Symbol)->Name.c_str(),
69+
*((*R.Symbol)->section()),
7070
Sec->CanonicalName.c_str());
7171
SymTable.removeSymbols(IsDead);
7272
for (std::unique_ptr<SymbolEntry> &S : SymTable.Symbols)

llvm/tools/llvm-objcopy/MachO/Object.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,14 @@ struct StringTable {
162162
};
163163

164164
struct RelocationInfo {
165-
const SymbolEntry *Symbol;
165+
// The referenced symbol entry. Set if !Scattered && Extern.
166+
Optional<const SymbolEntry *> Symbol;
167+
// The referenced section. Set if !Scattered && !Extern.
168+
Optional<const Section *> Section;
166169
// True if Info is a scattered_relocation_info.
167170
bool Scattered;
171+
// True if the r_symbolnum points to a section number (i.e. r_extern=0).
172+
bool Extern;
168173
MachO::any_relocation_info Info;
169174

170175
unsigned getPlainRelocationSymbolNum(bool IsLittleEndian) {

0 commit comments

Comments
 (0)