-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[NFC] Split portions of DWARFDataExtractor into new class #140096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Sterling-Augustine
merged 5 commits into
llvm:main
from
Sterling-Augustine:data_extractor
Jun 6, 2025
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
174545c
[NFC] Split portions of DWARFDataExtractor into DWARFDataExtractorSim…
Sterling-Augustine f946489
Address comments
Sterling-Augustine 3257479
Merge branch 'main' into data_extractor
Sterling-Augustine 56bb603
Address comments.
Sterling-Augustine 1351eba
Fix comment
Sterling-Augustine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -14,55 +14,42 @@ | |
|
|
||
| #include "llvm/BinaryFormat/Dwarf.h" | ||
| #include "llvm/Support/DataExtractor.h" | ||
| #include "llvm/Support/Errc.h" | ||
| #include "llvm/Support/MathExtras.h" | ||
|
|
||
| namespace llvm { | ||
|
|
||
| /// A DataExtractor suitable use for parsing dwarf from memory with minimal | ||
| /// dwarf context--no sections and no objects. getRelocated* functions | ||
| /// return raw values. | ||
| /// A DataExtractor suitable use for parsing dwarf from memory. Clients use | ||
| /// Relocator::getRelocatedValueImpl to relocate values as appropriate. | ||
|
|
||
| class DWARFDataExtractorSimple : public DataExtractor { | ||
| template <class Relocator> class DWARFDataExtractorBase : public DataExtractor { | ||
|
|
||
| public: | ||
| DWARFDataExtractorSimple(StringRef Data, bool IsLittleEndian, | ||
| uint8_t AddressSize) | ||
| DWARFDataExtractorBase(StringRef Data, bool IsLittleEndian, | ||
| uint8_t AddressSize) | ||
| : DataExtractor(Data, IsLittleEndian, AddressSize) {} | ||
| DWARFDataExtractorSimple(ArrayRef<uint8_t> Data, bool IsLittleEndian, | ||
| uint8_t AddressSize) | ||
| DWARFDataExtractorBase(ArrayRef<uint8_t> Data, bool IsLittleEndian, | ||
| uint8_t AddressSize) | ||
| : DataExtractor( | ||
| StringRef(reinterpret_cast<const char *>(Data.data()), Data.size()), | ||
| IsLittleEndian, AddressSize) {} | ||
|
|
||
| /// Truncating constructor | ||
| DWARFDataExtractorSimple(const DWARFDataExtractorSimple &Other, size_t Length) | ||
| DWARFDataExtractorBase(const DWARFDataExtractorBase &Other, size_t Length) | ||
| : DataExtractor(Other.getData().substr(0, Length), Other.isLittleEndian(), | ||
| Other.getAddressSize()) {} | ||
|
|
||
| virtual ~DWARFDataExtractorSimple() = default; | ||
| ~DWARFDataExtractorBase() {} | ||
|
||
|
|
||
| /// Extracts the DWARF "initial length" field, which can either be a 32-bit | ||
| /// value smaller than 0xfffffff0, or the value 0xffffffff followed by a | ||
| /// 64-bit length. Returns the actual length, and the DWARF format which is | ||
| /// encoded in the field. In case of errors, it returns {0, DWARF32} and | ||
| /// leaves the offset unchanged. | ||
| std::pair<uint64_t, dwarf::DwarfFormat> | ||
| getInitialLength(uint64_t *Off, Error *Err = nullptr) const; | ||
|
|
||
| std::pair<uint64_t, dwarf::DwarfFormat> getInitialLength(Cursor &C) const { | ||
| return getInitialLength(&getOffset(C), &getError(C)); | ||
| /// Extracts a value and returns it as adjusted by the Relocator | ||
| uint64_t getRelocatedValue(uint32_t Size, uint64_t *Off, | ||
| uint64_t *SectionIndex = nullptr, | ||
| Error *Err = nullptr) const { | ||
| return static_cast<const Relocator *>(this)->getRelocatedValueImpl( | ||
| Size, Off, SectionIndex, Err); | ||
| } | ||
|
|
||
| /// Extracts a value and returns it unrelocated. Named such to implement the | ||
| /// required interface. | ||
| virtual uint64_t getRelocatedValue(uint32_t Size, uint64_t *Off, | ||
| uint64_t *SectionIndex = nullptr, | ||
| Error *Err = nullptr) const { | ||
| assert(SectionIndex == nullptr && | ||
| "DWARFDATAExtractorSimple cannot take section indices."); | ||
| return getUnsigned(Off, Size, Err); | ||
| } | ||
| virtual uint64_t getRelocatedValue(Cursor &C, uint32_t Size, | ||
| uint64_t *SectionIndex = nullptr) const { | ||
| uint64_t getRelocatedValue(Cursor &C, uint32_t Size, | ||
| uint64_t *SectionIndex = nullptr) const { | ||
| return getRelocatedValue(Size, &getOffset(C), SectionIndex, &getError(C)); | ||
| } | ||
|
|
||
|
|
@@ -75,14 +62,131 @@ class DWARFDataExtractorSimple : public DataExtractor { | |
| &getError(C)); | ||
| } | ||
|
|
||
| /// Extracts the DWARF "initial length" field, which can either be a 32-bit | ||
| /// value smaller than 0xfffffff0, or the value 0xffffffff followed by a | ||
| /// 64-bit length. Returns the actual length, and the DWARF format which is | ||
| /// encoded in the field. In case of errors, it returns {0, DWARF32} and | ||
| /// leaves the offset unchanged. | ||
| std::pair<uint64_t, dwarf::DwarfFormat> | ||
| getInitialLength(uint64_t *Off, Error *Err = nullptr) const { | ||
| ErrorAsOutParameter ErrAsOut(Err); | ||
| if (Err && *Err) | ||
| return {0, dwarf::DWARF32}; | ||
|
|
||
| Cursor C(*Off); | ||
| uint64_t Length = getRelocatedValue(C, 4); | ||
| dwarf::DwarfFormat Format = dwarf::DWARF32; | ||
| if (Length == dwarf::DW_LENGTH_DWARF64) { | ||
| Length = getRelocatedValue(C, 8); | ||
| Format = dwarf::DWARF64; | ||
| } else if (Length >= dwarf::DW_LENGTH_lo_reserved) { | ||
| cantFail(C.takeError()); | ||
| if (Err) | ||
| *Err = createStringError( | ||
| std::errc::invalid_argument, | ||
| "unsupported reserved unit length of value 0x%8.8" PRIx64, Length); | ||
| return {0, dwarf::DWARF32}; | ||
| } | ||
|
|
||
| if (C) { | ||
| *Off = C.tell(); | ||
| return {Length, Format}; | ||
| } | ||
| if (Err) | ||
| *Err = C.takeError(); | ||
| else | ||
| consumeError(C.takeError()); | ||
| return {0, dwarf::DWARF32}; | ||
| } | ||
|
|
||
| std::pair<uint64_t, dwarf::DwarfFormat> getInitialLength(Cursor &C) const { | ||
| return getInitialLength(&getOffset(C), &getError(C)); | ||
| } | ||
|
|
||
| /// Extracts a DWARF-encoded pointer in \p Offset using \p Encoding. | ||
| /// There is a DWARF encoding that uses a PC-relative adjustment. | ||
| /// For these values, \p AbsPosOffset is used to fix them, which should | ||
| /// reflect the absolute address of this pointer. | ||
| std::optional<uint64_t> getEncodedPointer(uint64_t *Offset, uint8_t Encoding, | ||
| uint64_t AbsPosOffset = 0) const; | ||
| uint64_t PCRelOffset) const { | ||
| if (Encoding == dwarf::DW_EH_PE_omit) | ||
| return std::nullopt; | ||
|
|
||
| uint64_t Result = 0; | ||
| uint64_t OldOffset = *Offset; | ||
| // First get value | ||
| switch (Encoding & 0x0F) { | ||
| case dwarf::DW_EH_PE_absptr: | ||
| switch (getAddressSize()) { | ||
| case 2: | ||
| case 4: | ||
| case 8: | ||
| Result = getUnsigned(Offset, getAddressSize()); | ||
| break; | ||
| default: | ||
| return std::nullopt; | ||
| } | ||
| break; | ||
| case dwarf::DW_EH_PE_uleb128: | ||
| Result = getULEB128(Offset); | ||
| break; | ||
| case dwarf::DW_EH_PE_sleb128: | ||
| Result = getSLEB128(Offset); | ||
| break; | ||
| case dwarf::DW_EH_PE_udata2: | ||
| Result = getUnsigned(Offset, 2); | ||
| break; | ||
| case dwarf::DW_EH_PE_udata4: | ||
| Result = getUnsigned(Offset, 4); | ||
| break; | ||
| case dwarf::DW_EH_PE_udata8: | ||
| Result = getUnsigned(Offset, 8); | ||
| break; | ||
| case dwarf::DW_EH_PE_sdata2: | ||
| Result = getSigned(Offset, 2); | ||
| break; | ||
| case dwarf::DW_EH_PE_sdata4: | ||
| Result = SignExtend64<32>(getRelocatedValue(4, Offset)); | ||
| break; | ||
| case dwarf::DW_EH_PE_sdata8: | ||
| Result = getRelocatedValue(8, Offset); | ||
| break; | ||
| default: | ||
| return std::nullopt; | ||
| } | ||
| // Then add relative offset, if required | ||
| switch (Encoding & 0x70) { | ||
| case dwarf::DW_EH_PE_absptr: | ||
| // do nothing | ||
| break; | ||
| case dwarf::DW_EH_PE_pcrel: | ||
| Result += PCRelOffset; | ||
| break; | ||
| case dwarf::DW_EH_PE_datarel: | ||
| case dwarf::DW_EH_PE_textrel: | ||
| case dwarf::DW_EH_PE_funcrel: | ||
| case dwarf::DW_EH_PE_aligned: | ||
| default: | ||
| *Offset = OldOffset; | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| return Result; | ||
| } | ||
| }; | ||
|
|
||
| } // end namespace llvm | ||
| class DWARFDataExtractorSimple | ||
| : public DWARFDataExtractorBase<DWARFDataExtractorSimple> { | ||
| using DWARFDataExtractorBase::DWARFDataExtractorBase; | ||
|
|
||
| uint64_t getRelocatedValueImpl(uint32_t Size, uint64_t *Off, | ||
| uint64_t *SectionIndex = nullptr, | ||
| Error *Err = nullptr) const { | ||
| assert(SectionIndex == nullptr && | ||
| "DWARFDATAExtractorSimple cannot take section indices."); | ||
| return getUnsigned(Off, Size, Err); | ||
| } | ||
| }; | ||
|
|
||
| } // end namespace llvm | ||
| #endif // LLVM_DEBUGINFO_DWARF_DWARFDATAEXTRACTOR_H | ||
This file contains hidden or 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 was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think we have a documented convention, but at least in llvm/include, there's more use of
typenamein template argument lists thanclass(at a very dodgy grepping, at least... ) - I'd tend to favor that.