Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update DWARF lexical blocks address ranges.
Summary: Updates DWARF lexical blocks address ranges in the output binary after optimizations. This is similar to updating function address ranges except that the ranges representation needs to be more general, since address ranges can begin or end in the middle of a basic block. The following changes were made: - Added a data structure for iterating over the basic blocks that intersect an address range: BasicBlockTable.h - Added some more bookkeeping in BinaryBasicBlock. Basically, I needed to keep track of the block's size in the input binary as well as its address in the output binary. This information is mostly set by BinaryFunction after disassembly. - Added a representation for address ranges relative to basic blocks (BasicBlockOffsetRanges.h). Will also serve for location lists. - Added a representation for Lexical Blocks (LexicalBlock.h) - Small refactorings in DebugArangesWriter: -- Renamed to DebugRangesSectionsWriter since it also writes .debug_ranges -- Refactored it not to depend on BinaryFunction but instead on anything that can be assined an aoffset in .debug_ranges (added an interface for that) - Iterate over the DIE tree during initialization to find lexical blocks in .debug_info (BinaryContext.cpp) - Added patches to .debug_abbrev and .debug_info in RewriteInstance to update lexical blocks attributes (in fact, this part is very similar to what was done to function address ranges and I just refactored/reused that code) - Added small test case (lexical_blocks_address_ranges_debug.test) (cherry picked from FBD3113181)
- Loading branch information
Showing
14 changed files
with
512 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//===- BasicBlockOffsetRanges.cpp - list of address ranges relative to BBs ===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "BasicBlockOffsetRanges.h" | ||
#include "BinaryBasicBlock.h" | ||
#include "BinaryFunction.h" | ||
|
||
namespace llvm { | ||
namespace bolt { | ||
|
||
void BasicBlockOffsetRanges::addAddressRange(BinaryFunction &Function, | ||
uint64_t BeginAddress, | ||
uint64_t EndAddress) { | ||
auto FirstBB = Function.getBasicBlockContainingOffset( | ||
BeginAddress - Function.getAddress()); | ||
assert(FirstBB && "No basic blocks in the function intersect given range."); | ||
|
||
for (auto I = Function.getIndex(FirstBB), S = Function.size(); I != S; ++I) { | ||
auto BB = Function.getBasicBlockAtIndex(I); | ||
uint64_t BBAddress = Function.getAddress() + BB->getOffset(); | ||
if (BBAddress >= EndAddress) | ||
break; | ||
|
||
uint64_t InternalAddressRangeBegin = std::max(BBAddress, BeginAddress); | ||
assert(BB->getFunction() == &Function && | ||
"Mismatching functions.\n"); | ||
uint64_t InternalAddressRangeEnd = | ||
std::min(BBAddress + Function.getBasicBlockOriginalSize(BB), | ||
EndAddress); | ||
|
||
AddressRanges.push_back( | ||
BBAddressRange{ | ||
BB, | ||
static_cast<uint16_t>(InternalAddressRangeBegin - BBAddress), | ||
static_cast<uint16_t>(InternalAddressRangeEnd - BBAddress)}); | ||
} | ||
} | ||
|
||
std::vector<std::pair<uint64_t, uint64_t>> | ||
BasicBlockOffsetRanges::getAbsoluteAddressRanges() const { | ||
std::vector<std::pair<uint64_t, uint64_t>> AbsoluteRanges; | ||
for (const auto &BBAddressRange : AddressRanges) { | ||
auto BBOutputAddressRange = | ||
BBAddressRange.BasicBlock->getOutputAddressRange(); | ||
uint64_t NewRangeBegin = BBOutputAddressRange.first + | ||
BBAddressRange.RangeBeginOffset; | ||
// If the end offset pointed to the end of the basic block, then we set | ||
// the new end range to cover the whole basic block as the BB's size | ||
// might have increased. | ||
auto BBFunction = BBAddressRange.BasicBlock->getFunction(); | ||
uint64_t NewRangeEnd = | ||
(BBAddressRange.RangeEndOffset == | ||
BBFunction->getBasicBlockOriginalSize(BBAddressRange.BasicBlock)) | ||
? BBOutputAddressRange.second | ||
: (BBOutputAddressRange.first + BBAddressRange.RangeEndOffset); | ||
AbsoluteRanges.emplace_back(NewRangeBegin, NewRangeEnd); | ||
} | ||
return AbsoluteRanges; | ||
} | ||
|
||
} // namespace bolt | ||
} // namespace llvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//===--- BasicBlockOffsetRanges.h - list of address ranges relative to BBs ===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Represents a list of address ranges where addresses are relative to the | ||
// beginning of basic blocks. Useful for converting address ranges in the input | ||
// binary to equivalent ranges after optimizations take place. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_TOOLS_LLVM_BOLT_BASIC_BLOCK_OFFSET_RANGES_H | ||
#define LLVM_TOOLS_LLVM_BOLT_BASIC_BLOCK_OFFSET_RANGES_H | ||
|
||
#include <map> | ||
#include <utility> | ||
#include <vector> | ||
|
||
namespace llvm { | ||
namespace bolt { | ||
|
||
class BinaryFunction; | ||
class BinaryBasicBlock; | ||
|
||
class BasicBlockOffsetRanges { | ||
private: | ||
/// An address range inside one basic block. | ||
struct BBAddressRange { | ||
const BinaryBasicBlock *BasicBlock; | ||
/// Beginning of the range counting from BB's start address. | ||
uint16_t RangeBeginOffset; | ||
/// (Exclusive) end of the range counting from BB's start address. | ||
uint16_t RangeEndOffset; | ||
}; | ||
|
||
std::vector<BBAddressRange> AddressRanges; | ||
|
||
public: | ||
/// Add range [BeginAddress, EndAddress) to the address ranges list. | ||
/// \p Function is the function that contains the given address range. | ||
void addAddressRange(BinaryFunction &Function, | ||
uint64_t BeginAddress, | ||
uint64_t EndAddress); | ||
|
||
/// Returns the list of absolute addresses calculated using the output address | ||
/// of the basic blocks, i.e. the input ranges updated after basic block | ||
/// addresses might have changed. | ||
std::vector<std::pair<uint64_t, uint64_t>> getAbsoluteAddressRanges() const; | ||
}; | ||
|
||
} // namespace bolt | ||
} // namespace llvm | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.