Skip to content

Commit 49c52e7

Browse files
committed
[BOLT] Decouple new segment creation from PHDR rewrite
Refactor handling of PHDR table rewrite to make modifications easier.
1 parent d00c83e commit 49c52e7

File tree

5 files changed

+138
-125
lines changed

5 files changed

+138
-125
lines changed

bolt/include/bolt/Core/BinaryContext.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,15 @@ struct SegmentInfo {
7373
uint64_t FileSize; /// Size in file.
7474
uint64_t Alignment; /// Alignment of the segment.
7575
bool IsExecutable; /// Is the executable bit set on the Segment?
76+
bool IsWritable; /// Is the segment writable.
7677

7778
void print(raw_ostream &OS) const {
7879
OS << "SegmentInfo { Address: 0x" << Twine::utohexstr(Address)
7980
<< ", Size: 0x" << Twine::utohexstr(Size) << ", FileOffset: 0x"
8081
<< Twine::utohexstr(FileOffset) << ", FileSize: 0x"
8182
<< Twine::utohexstr(FileSize) << ", Alignment: 0x"
82-
<< Twine::utohexstr(Alignment) << ", " << (IsExecutable ? "x" : " ")
83-
<< "}";
83+
<< Twine::utohexstr(Alignment) << ", " << (IsExecutable ? "x" : "")
84+
<< (IsWritable ? "w" : "") << " }";
8485
};
8586
};
8687

@@ -333,9 +334,14 @@ class BinaryContext {
333334
std::optional<StringRef> Source,
334335
unsigned CUID, unsigned DWARFVersion);
335336

337+
/// Input file segment info
338+
///
336339
/// [start memory address] -> [segment info] mapping.
337340
std::map<uint64_t, SegmentInfo> SegmentMapInfo;
338341

342+
/// Newly created segments.
343+
std::vector<SegmentInfo> NewSegments;
344+
339345
/// Symbols that are expected to be undefined in MCContext during emission.
340346
std::unordered_set<MCSymbol *> UndefinedSymbols;
341347

bolt/include/bolt/Rewrite/RewriteInstance.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ class RewriteInstance {
297297
return FUNC(ELF64BE); \
298298
}
299299

300+
/// Update loadable segment information based on new sections.
301+
void updateSegmentInfo();
302+
300303
/// Patch ELF book-keeping info.
301304
void patchELFPHDRTable();
302305

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 102 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,14 @@ Error RewriteInstance::discoverStorage() {
547547
NextAvailableOffset = std::max(NextAvailableOffset,
548548
Phdr.p_offset + Phdr.p_filesz);
549549

550-
BC->SegmentMapInfo[Phdr.p_vaddr] = SegmentInfo{
551-
Phdr.p_vaddr, Phdr.p_memsz, Phdr.p_offset,
552-
Phdr.p_filesz, Phdr.p_align, ((Phdr.p_flags & ELF::PF_X) != 0)};
550+
BC->SegmentMapInfo[Phdr.p_vaddr] =
551+
SegmentInfo{Phdr.p_vaddr,
552+
Phdr.p_memsz,
553+
Phdr.p_offset,
554+
Phdr.p_filesz,
555+
Phdr.p_align,
556+
(Phdr.p_flags & ELF::PF_X) != 0,
557+
(Phdr.p_flags & ELF::PF_W) != 0};
553558
if (BC->TheTriple->getArch() == llvm::Triple::x86_64 &&
554559
Phdr.p_vaddr >= BinaryContext::KernelStartX86_64)
555560
BC->IsLinuxKernel = true;
@@ -4155,6 +4160,74 @@ void RewriteInstance::updateOutputValues(const BOLTLinker &Linker) {
41554160
Function->updateOutputValues(Linker);
41564161
}
41574162

4163+
void RewriteInstance::updateSegmentInfo() {
4164+
// NOTE Currently .eh_frame_hdr appends to the last segment, recalculate
4165+
// last segments size based on the NextAvailableAddress variable.
4166+
if (!NewWritableSegmentSize) {
4167+
if (NewTextSegmentAddress)
4168+
NewTextSegmentSize = NextAvailableAddress - NewTextSegmentAddress;
4169+
} else {
4170+
NewWritableSegmentSize = NextAvailableAddress - NewWritableSegmentAddress;
4171+
}
4172+
4173+
if (NewTextSegmentSize) {
4174+
SegmentInfo TextSegment = {NewTextSegmentAddress,
4175+
NewTextSegmentSize,
4176+
NewTextSegmentOffset,
4177+
NewTextSegmentSize,
4178+
BC->PageAlign,
4179+
true,
4180+
false};
4181+
if (!opts::Instrument) {
4182+
BC->NewSegments.push_back(TextSegment);
4183+
} else {
4184+
ErrorOr<BinarySection &> Sec =
4185+
BC->getUniqueSectionByName(".bolt.instr.counters");
4186+
assert(Sec && "expected one and only one `.bolt.instr.counters` section");
4187+
const uint64_t Addr = Sec->getOutputAddress();
4188+
const uint64_t Offset = Sec->getOutputFileOffset();
4189+
const uint64_t Size = Sec->getOutputSize();
4190+
assert(Addr > TextSegment.Address &&
4191+
Addr + Size < TextSegment.Address + TextSegment.Size &&
4192+
"`.bolt.instr.counters` section is expected to be included in the "
4193+
"new text segment");
4194+
4195+
// Set correct size for the previous header since we are breaking the
4196+
// new text segment into three segments.
4197+
uint64_t Delta = Addr - TextSegment.Address;
4198+
TextSegment.Size = Delta;
4199+
TextSegment.FileSize = Delta;
4200+
BC->NewSegments.push_back(TextSegment);
4201+
4202+
// Create RW segment that includes the `.bolt.instr.counters` section.
4203+
SegmentInfo RWSegment = {Addr, Size, Offset, Size, BC->RegularPageSize,
4204+
false, true};
4205+
BC->NewSegments.push_back(RWSegment);
4206+
4207+
// Create RX segment that includes all RX sections from runtime library.
4208+
const uint64_t AddrRX = alignTo(Addr + Size, BC->RegularPageSize);
4209+
const uint64_t OffsetRX = alignTo(Offset + Size, BC->RegularPageSize);
4210+
const uint64_t SizeRX =
4211+
NewTextSegmentSize - (AddrRX - TextSegment.Address);
4212+
SegmentInfo RXSegment = {
4213+
AddrRX, SizeRX, OffsetRX, SizeRX, BC->RegularPageSize, true, false};
4214+
BC->NewSegments.push_back(RXSegment);
4215+
}
4216+
}
4217+
4218+
if (NewWritableSegmentSize) {
4219+
SegmentInfo DataSegmentInfo = {
4220+
NewWritableSegmentAddress,
4221+
NewWritableSegmentSize,
4222+
getFileOffsetForAddress(NewWritableSegmentAddress),
4223+
NewWritableSegmentSize,
4224+
BC->RegularPageSize,
4225+
false,
4226+
true};
4227+
BC->NewSegments.push_back(DataSegmentInfo);
4228+
}
4229+
}
4230+
41584231
void RewriteInstance::patchELFPHDRTable() {
41594232
auto ELF64LEFile = cast<ELF64LEObjectFile>(InputFile);
41604233
const ELFFile<ELF64LE> &Obj = ELF64LEFile->getELFFile();
@@ -4181,107 +4254,36 @@ void RewriteInstance::patchELFPHDRTable() {
41814254
if (opts::Instrument)
41824255
Phnum += 2;
41834256

4184-
// NOTE Currently .eh_frame_hdr appends to the last segment, recalculate
4185-
// last segments size based on the NextAvailableAddress variable.
4186-
if (!NewWritableSegmentSize) {
4187-
if (NewTextSegmentAddress)
4188-
NewTextSegmentSize = NextAvailableAddress - NewTextSegmentAddress;
4189-
} else {
4190-
NewWritableSegmentSize = NextAvailableAddress - NewWritableSegmentAddress;
4191-
}
4192-
4193-
if (!NewTextSegmentSize && !NewWritableSegmentSize) {
4257+
if (BC->NewSegments.empty()) {
41944258
BC->outs() << "BOLT-INFO: not adding new segments\n";
41954259
return;
41964260
}
41974261

41984262
const uint64_t SavedPos = OS.tell();
41994263
OS.seek(PHDRTableOffset);
42004264

4201-
auto createNewPhdrs = [&]() {
4202-
SmallVector<ELF64LEPhdrTy, 3> NewPhdrs;
4203-
ELF64LEPhdrTy NewPhdr;
4204-
NewPhdr.p_type = ELF::PT_LOAD;
4205-
NewPhdr.p_offset = NewTextSegmentOffset;
4206-
NewPhdr.p_vaddr = NewTextSegmentAddress;
4207-
NewPhdr.p_paddr = NewTextSegmentAddress;
4208-
NewPhdr.p_filesz = NewTextSegmentSize;
4209-
NewPhdr.p_memsz = NewTextSegmentSize;
4210-
NewPhdr.p_flags = ELF::PF_X | ELF::PF_R;
4211-
NewPhdr.p_align = BC->PageAlign;
4212-
4213-
if (!opts::Instrument) {
4214-
NewPhdrs.push_back(NewPhdr);
4215-
} else {
4216-
ErrorOr<BinarySection &> Sec =
4217-
BC->getUniqueSectionByName(".bolt.instr.counters");
4218-
assert(Sec && "expected one and only one `.bolt.instr.counters` section");
4219-
const uint64_t Addr = Sec->getOutputAddress();
4220-
const uint64_t Offset = Sec->getOutputFileOffset();
4221-
const uint64_t Size = Sec->getOutputSize();
4222-
assert(Addr > NewPhdr.p_vaddr &&
4223-
Addr + Size < NewPhdr.p_vaddr + NewPhdr.p_memsz &&
4224-
"`.bolt.instr.counters` section is expected to be included in the "
4225-
"new text sgement");
4226-
4227-
// Set correct size for the previous header since we are breaking the
4228-
// new text segment into three segments.
4229-
uint64_t Delta = Addr - NewPhdr.p_vaddr;
4230-
NewPhdr.p_filesz = Delta;
4231-
NewPhdr.p_memsz = Delta;
4232-
NewPhdrs.push_back(NewPhdr);
4233-
4234-
// Create a program header for a RW segment that includes the
4235-
// `.bolt.instr.counters` section only.
4236-
ELF64LEPhdrTy NewPhdrRWSegment;
4237-
NewPhdrRWSegment.p_type = ELF::PT_LOAD;
4238-
NewPhdrRWSegment.p_offset = Offset;
4239-
NewPhdrRWSegment.p_vaddr = Addr;
4240-
NewPhdrRWSegment.p_paddr = Addr;
4241-
NewPhdrRWSegment.p_filesz = Size;
4242-
NewPhdrRWSegment.p_memsz = Size;
4243-
NewPhdrRWSegment.p_flags = ELF::PF_R | ELF::PF_W;
4244-
NewPhdrRWSegment.p_align = BC->RegularPageSize;
4245-
NewPhdrs.push_back(NewPhdrRWSegment);
4246-
4247-
// Create a program header for a RX segment that includes all the RX
4248-
// sections from runtime library.
4249-
ELF64LEPhdrTy NewPhdrRXSegment;
4250-
NewPhdrRXSegment.p_type = ELF::PT_LOAD;
4251-
const uint64_t AddrRX = alignTo(Addr + Size, BC->RegularPageSize);
4252-
const uint64_t OffsetRX = alignTo(Offset + Size, BC->RegularPageSize);
4253-
const uint64_t SizeRX = NewTextSegmentSize - (AddrRX - NewPhdr.p_paddr);
4254-
NewPhdrRXSegment.p_offset = OffsetRX;
4255-
NewPhdrRXSegment.p_vaddr = AddrRX;
4256-
NewPhdrRXSegment.p_paddr = AddrRX;
4257-
NewPhdrRXSegment.p_filesz = SizeRX;
4258-
NewPhdrRXSegment.p_memsz = SizeRX;
4259-
NewPhdrRXSegment.p_flags = ELF::PF_X | ELF::PF_R;
4260-
NewPhdrRXSegment.p_align = BC->RegularPageSize;
4261-
NewPhdrs.push_back(NewPhdrRXSegment);
4262-
}
4263-
4264-
return NewPhdrs;
4265+
auto createPhdr = [](const SegmentInfo &SI) {
4266+
ELF64LEPhdrTy Phdr;
4267+
Phdr.p_type = ELF::PT_LOAD;
4268+
Phdr.p_offset = SI.FileOffset;
4269+
Phdr.p_vaddr = SI.Address;
4270+
Phdr.p_paddr = SI.Address;
4271+
Phdr.p_filesz = SI.FileSize;
4272+
Phdr.p_memsz = SI.Size;
4273+
Phdr.p_flags = ELF::PF_R;
4274+
if (SI.IsExecutable)
4275+
Phdr.p_flags |= ELF::PF_X;
4276+
if (SI.IsWritable)
4277+
Phdr.p_flags |= ELF::PF_W;
4278+
Phdr.p_align = SI.Alignment;
4279+
4280+
return Phdr;
42654281
};
42664282

42674283
auto writeNewSegmentPhdrs = [&]() {
4268-
if (NewTextSegmentSize) {
4269-
SmallVector<ELF64LE::Phdr, 3> NewPhdrs = createNewPhdrs();
4270-
OS.write(reinterpret_cast<const char *>(NewPhdrs.data()),
4271-
sizeof(ELF64LE::Phdr) * NewPhdrs.size());
4272-
}
4273-
4274-
if (NewWritableSegmentSize) {
4275-
ELF64LEPhdrTy NewPhdr;
4276-
NewPhdr.p_type = ELF::PT_LOAD;
4277-
NewPhdr.p_offset = getFileOffsetForAddress(NewWritableSegmentAddress);
4278-
NewPhdr.p_vaddr = NewWritableSegmentAddress;
4279-
NewPhdr.p_paddr = NewWritableSegmentAddress;
4280-
NewPhdr.p_filesz = NewWritableSegmentSize;
4281-
NewPhdr.p_memsz = NewWritableSegmentSize;
4282-
NewPhdr.p_align = BC->RegularPageSize;
4283-
NewPhdr.p_flags = ELF::PF_R | ELF::PF_W;
4284-
OS.write(reinterpret_cast<const char *>(&NewPhdr), sizeof(NewPhdr));
4284+
for (const SegmentInfo &SI : BC->NewSegments) {
4285+
ELF64LEPhdrTy Phdr = createPhdr(SI);
4286+
OS.write(reinterpret_cast<const char *>(&Phdr), sizeof(Phdr));
42854287
}
42864288
};
42874289

@@ -4317,11 +4319,9 @@ void RewriteInstance::patchELFPHDRTable() {
43174319
case ELF::PT_GNU_STACK:
43184320
if (opts::UseGnuStack) {
43194321
// Overwrite the header with the new segment header.
4320-
assert(!opts::Instrument);
4321-
SmallVector<ELF64LE::Phdr, 3> NewPhdrs = createNewPhdrs();
4322-
assert(NewPhdrs.size() == 1 &&
4323-
"expect exactly one program header was created");
4324-
NewPhdr = NewPhdrs[0];
4322+
assert(BC->NewSegments.size() == 1 &&
4323+
"Expected exactly one new segment");
4324+
NewPhdr = createPhdr(BC->NewSegments.front());
43254325
ModdedGnuStack = true;
43264326
}
43274327
break;
@@ -5946,8 +5946,10 @@ void RewriteInstance::rewriteFile() {
59465946
addBATSection();
59475947

59485948
// Patch program header table.
5949-
if (!BC->IsLinuxKernel)
5949+
if (!BC->IsLinuxKernel) {
5950+
updateSegmentInfo();
59505951
patchELFPHDRTable();
5952+
}
59515953

59525954
// Finalize memory image of section string table.
59535955
finalizeSectionStringTable();

bolt/unittests/Core/BinaryContext.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,13 @@ TEST_P(BinaryContextTester, BaseAddress) {
199199
// Check that base address calculation is correct for a binary with the
200200
// following segment layout:
201201
BC->SegmentMapInfo[0] =
202-
SegmentInfo{0, 0x10e8c2b4, 0, 0x10e8c2b4, 0x1000, true};
203-
BC->SegmentMapInfo[0x10e8d2b4] =
204-
SegmentInfo{0x10e8d2b4, 0x3952faec, 0x10e8c2b4, 0x3952faec, 0x1000, true};
205-
BC->SegmentMapInfo[0x4a3bddc0] =
206-
SegmentInfo{0x4a3bddc0, 0x148e828, 0x4a3bbdc0, 0x148e828, 0x1000, true};
207-
BC->SegmentMapInfo[0x4b84d5e8] =
208-
SegmentInfo{0x4b84d5e8, 0x294f830, 0x4b84a5e8, 0x3d3820, 0x1000, true};
202+
SegmentInfo{0, 0x10e8c2b4, 0, 0x10e8c2b4, 0x1000, true, false};
203+
BC->SegmentMapInfo[0x10e8d2b4] = SegmentInfo{
204+
0x10e8d2b4, 0x3952faec, 0x10e8c2b4, 0x3952faec, 0x1000, true, false};
205+
BC->SegmentMapInfo[0x4a3bddc0] = SegmentInfo{
206+
0x4a3bddc0, 0x148e828, 0x4a3bbdc0, 0x148e828, 0x1000, true, false};
207+
BC->SegmentMapInfo[0x4b84d5e8] = SegmentInfo{
208+
0x4b84d5e8, 0x294f830, 0x4b84a5e8, 0x3d3820, 0x1000, true, false};
209209

210210
std::optional<uint64_t> BaseAddress =
211211
BC->getBaseAddressForMapping(0x7f13f5556000, 0x10e8c000);
@@ -220,13 +220,14 @@ TEST_P(BinaryContextTester, BaseAddress2) {
220220
// Check that base address calculation is correct for a binary if the
221221
// alignment in ELF file are different from pagesize.
222222
// The segment layout is as follows:
223-
BC->SegmentMapInfo[0] = SegmentInfo{0, 0x2177c, 0, 0x2177c, 0x10000, true};
223+
BC->SegmentMapInfo[0] =
224+
SegmentInfo{0, 0x2177c, 0, 0x2177c, 0x10000, true, false};
224225
BC->SegmentMapInfo[0x31860] =
225-
SegmentInfo{0x31860, 0x370, 0x21860, 0x370, 0x10000, true};
226+
SegmentInfo{0x31860, 0x370, 0x21860, 0x370, 0x10000, true, false};
226227
BC->SegmentMapInfo[0x41c20] =
227-
SegmentInfo{0x41c20, 0x1f8, 0x21c20, 0x1f8, 0x10000, true};
228+
SegmentInfo{0x41c20, 0x1f8, 0x21c20, 0x1f8, 0x10000, true, false};
228229
BC->SegmentMapInfo[0x54e18] =
229-
SegmentInfo{0x54e18, 0x51, 0x24e18, 0x51, 0x10000, true};
230+
SegmentInfo{0x54e18, 0x51, 0x24e18, 0x51, 0x10000, true, false};
230231

231232
std::optional<uint64_t> BaseAddress =
232233
BC->getBaseAddressForMapping(0xaaaaea444000, 0x21000);
@@ -242,13 +243,14 @@ TEST_P(BinaryContextTester, BaseAddressSegmentsSmallerThanAlignment) {
242243
// when multiple segments are close together in the ELF file (closer
243244
// than the required alignment in the process space).
244245
// See https://github.com/llvm/llvm-project/issues/109384
245-
BC->SegmentMapInfo[0] = SegmentInfo{0, 0x1d1c, 0, 0x1d1c, 0x10000, false};
246+
BC->SegmentMapInfo[0] =
247+
SegmentInfo{0, 0x1d1c, 0, 0x1d1c, 0x10000, false, false};
246248
BC->SegmentMapInfo[0x11d40] =
247-
SegmentInfo{0x11d40, 0x11e0, 0x1d40, 0x11e0, 0x10000, true};
249+
SegmentInfo{0x11d40, 0x11e0, 0x1d40, 0x11e0, 0x10000, true, false};
248250
BC->SegmentMapInfo[0x22f20] =
249-
SegmentInfo{0x22f20, 0x10e0, 0x2f20, 0x1f0, 0x10000, false};
251+
SegmentInfo{0x22f20, 0x10e0, 0x2f20, 0x1f0, 0x10000, false, false};
250252
BC->SegmentMapInfo[0x33110] =
251-
SegmentInfo{0x33110, 0x89, 0x3110, 0x88, 0x10000, false};
253+
SegmentInfo{0x33110, 0x89, 0x3110, 0x88, 0x10000, false, false};
252254

253255
std::optional<uint64_t> BaseAddress =
254256
BC->getBaseAddressForMapping(0xaaaaaaab1000, 0x1000);

bolt/unittests/Core/MemoryMaps.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ TEST_P(MemoryMapsTester, ParseMultipleSegments) {
100100
"[0xabc2000000(0x8000000) @ 0x31d0000 103:01 1573523 0]: r-xp {1}\n",
101101
Pid, Filename);
102102

103-
BC->SegmentMapInfo[0x11da000] =
104-
SegmentInfo{0x11da000, 0x10da000, 0x11ca000, 0x10da000, 0x10000, true};
105-
BC->SegmentMapInfo[0x31d0000] =
106-
SegmentInfo{0x31d0000, 0x51ac82c, 0x31d0000, 0x3000000, 0x200000, true};
103+
BC->SegmentMapInfo[0x11da000] = SegmentInfo{
104+
0x11da000, 0x10da000, 0x11ca000, 0x10da000, 0x10000, true, false};
105+
BC->SegmentMapInfo[0x31d0000] = SegmentInfo{
106+
0x31d0000, 0x51ac82c, 0x31d0000, 0x3000000, 0x200000, true, false};
107107

108108
DataAggregator DA("");
109109
BC->setFilename(Filename);
@@ -131,12 +131,12 @@ TEST_P(MemoryMapsTester, MultipleSegmentsMismatchedBaseAddress) {
131131
"[0xabc2000000(0x8000000) @ 0x31d0000 103:01 1573523 0]: r-xp {1}\n",
132132
Pid, Filename);
133133

134-
BC->SegmentMapInfo[0x11da000] =
135-
SegmentInfo{0x11da000, 0x10da000, 0x11ca000, 0x10da000, 0x10000, true};
134+
BC->SegmentMapInfo[0x11da000] = SegmentInfo{
135+
0x11da000, 0x10da000, 0x11ca000, 0x10da000, 0x10000, true, false};
136136
// Using '0x31d0fff' FileOffset which triggers a different base address
137137
// for this second text segment.
138-
BC->SegmentMapInfo[0x31d0000] =
139-
SegmentInfo{0x31d0000, 0x51ac82c, 0x31d0fff, 0x3000000, 0x200000, true};
138+
BC->SegmentMapInfo[0x31d0000] = SegmentInfo{
139+
0x31d0000, 0x51ac82c, 0x31d0fff, 0x3000000, 0x200000, true, false};
140140

141141
DataAggregator DA("");
142142
BC->setFilename(Filename);

0 commit comments

Comments
 (0)