LLVM 22.0.0git
ELFAttrParserCompact.cpp
Go to the documentation of this file.
1//===--- ELFCompactAttrParser.cpp - ELF Compact Attribute Parser ------ ===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7// ELF Compact Attribute Parser parse ELF build atrributes that are held
8// in a single Build Attributes Subsection.
9//
10//===--------------------------------------------------------------------===//
11
14#include "llvm/Support/Errc.h"
16
17using namespace llvm;
18using namespace llvm::ELFAttrs;
19
20static constexpr EnumEntry<unsigned> tagNames[] = {
21 {"Tag_File", ELFAttrs::File},
22 {"Tag_Section", ELFAttrs::Section},
23 {"Tag_Symbol", ELFAttrs::Symbol},
24};
25
27 const char *name, unsigned tag, ArrayRef<const char *> strings) {
29 if (value >= strings.size()) {
30 printAttribute(tag, value, "");
32 "unknown " + Twine(name) +
33 " value: " + Twine(value));
34 }
35 printAttribute(tag, value, strings[value]);
36 return Error::success();
37}
38
40 StringRef tagName =
41 ELFAttrs::attrTypeAsString(tag, tagToStringMap, /*hasTagPrefix=*/false);
43 attributes.insert(std::make_pair(tag, value));
44
45 if (sw) {
46 DictScope scope(*sw, "Attribute");
47 sw->printNumber("Tag", tag);
48 if (!tagName.empty())
49 sw->printString("TagName", tagName);
50 sw->printNumber("Value", value);
51 }
52 return Error::success();
53}
54
56 StringRef tagName =
57 ELFAttrs::attrTypeAsString(tag, tagToStringMap, /*hasTagPrefix=*/false);
59 setAttributeString(tag, desc);
60
61 if (sw) {
62 DictScope scope(*sw, "Attribute");
63 sw->printNumber("Tag", tag);
64 if (!tagName.empty())
65 sw->printString("TagName", tagName);
66 sw->printString("Value", desc);
67 }
68 return Error::success();
69}
70
71void ELFCompactAttrParser::printAttribute(unsigned tag, unsigned value,
72 StringRef valueDesc) {
73 attributes.insert(std::make_pair(tag, value));
74
75 if (sw) {
77 /*hasTagPrefix=*/false);
78 DictScope as(*sw, "Attribute");
79 sw->printNumber("Tag", tag);
80 sw->printNumber("Value", value);
81 if (!tagName.empty())
82 sw->printString("TagName", tagName);
83 if (!valueDesc.empty())
84 sw->printString("Description", valueDesc);
85 }
86}
87
89 for (;;) {
91 if (!cursor || !value)
92 break;
93 indexList.push_back(value);
94 }
95}
96
98 uint64_t pos;
99 uint64_t end = cursor.tell() + length;
100 while ((pos = cursor.tell()) < end) {
102 bool handled;
103 if (Error e = handler(tag, handled))
104 return e;
105
106 if (!handled) {
107 if (tag < 32) {
109 "invalid tag 0x" + Twine::utohexstr(tag) +
110 " at offset 0x" + Twine::utohexstr(pos));
111 }
112
113 if (tag % 2 == 0) {
114 if (Error e = integerAttribute(tag))
115 return e;
116 } else {
117 if (Error e = stringAttribute(tag))
118 return e;
119 }
120 }
121 }
122 return Error::success();
123}
124
126 uint64_t end = cursor.tell() - sizeof(length) + length;
127 StringRef vendorName = de.getCStrRef(cursor);
128 if (sw) {
129 sw->printNumber("SectionLength", length);
130 sw->printString("Vendor", vendorName);
131 }
132
133 // Handle a subsection with an unrecognized vendor-name by skipping
134 // over it to the next subsection. ADDENDA32 in the Arm ABI defines
135 // that vendor attribute sections must not affect compatibility, so
136 // this should always be safe.
137 if (vendorName.lower() != vendor) {
138 cursor.seek(end);
139 return Error::success();
140 }
141
142 while (cursor.tell() < end) {
143 /// Tag_File | Tag_Section | Tag_Symbol uleb128:byte-size
144 uint8_t tag = de.getU8(cursor);
146 if (!cursor)
147 return cursor.takeError();
148
149 if (sw) {
150 sw->printEnum("Tag", tag, ArrayRef(tagNames));
151 sw->printNumber("Size", size);
152 }
153 if (size < 5)
155 "invalid attribute size " + Twine(size) +
156 " at offset 0x" +
158
159 StringRef scopeName, indexName;
161 switch (tag) {
162 case ELFAttrs::File:
163 scopeName = "FileAttributes";
164 break;
166 scopeName = "SectionAttributes";
167 indexName = "Sections";
168 parseIndexList(indices);
169 break;
170 case ELFAttrs::Symbol:
171 scopeName = "SymbolAttributes";
172 indexName = "Symbols";
173 parseIndexList(indices);
174 break;
175 default:
177 "unrecognized tag 0x" + Twine::utohexstr(tag) +
178 " at offset 0x" +
180 }
181
182 if (sw) {
183 DictScope scope(*sw, scopeName);
184 if (!indices.empty())
185 sw->printList(indexName, indices);
186 if (Error e = parseAttributeList(size - 5))
187 return e;
188 } else if (Error e = parseAttributeList(size - 5))
189 return e;
190 }
191 return Error::success();
192}
193
196 unsigned sectionNumber = 0;
198
199 // For early returns, we have more specific errors, consume the Error in
200 // cursor.
201 struct ClearCursorError {
202 DataExtractor::Cursor &cursor;
203 ~ClearCursorError() { consumeError(cursor.takeError()); }
204 } clear{cursor};
205
206 // Unrecognized format-version.
207 uint8_t formatVersion = de.getU8(cursor);
208 if (formatVersion != ELFAttrs::Format_Version)
210 "unrecognized format-version: 0x" +
211 utohexstr(formatVersion));
212
213 while (!de.eof(cursor)) {
214 uint32_t sectionLength = de.getU32(cursor);
215 if (!cursor)
216 return cursor.takeError();
217
218 if (sw) {
219 sw->startLine() << "Section " << ++sectionNumber << " {\n";
220 sw->indent();
221 }
222
223 if (sectionLength < 4 || cursor.tell() - 4 + sectionLength > section.size())
225 "invalid section length " +
226 Twine(sectionLength) + " at offset 0x" +
227 utohexstr(cursor.tell() - 4));
228
229 if (Error e = parseSubsection(sectionLength))
230 return e;
231 if (sw) {
232 sw->unindent();
233 sw->startLine() << "}\n";
234 }
235 }
236
237 return cursor.takeError();
238}
Given that RA is a live value
static constexpr EnumEntry< unsigned > tagNames[]
static const char * name
Definition: SMEABIPass.cpp:52
This file contains some functions that are useful when dealing with strings.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition: DataExtractor.h:55
uint64_t tell() const
Return the current position of this Cursor.
Definition: DataExtractor.h:72
Error takeError()
Return error contained inside this Cursor, if any.
Definition: DataExtractor.h:79
void seek(uint64_t NewOffSet)
Set the cursor to the new offset. This does not impact the error state.
Definition: DataExtractor.h:75
LLVM_ABI uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
bool eof(const Cursor &C) const
Return true iff the cursor is at the end of the buffer, regardless of the error state of the cursor.
LLVM_ABI StringRef getCStrRef(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a C string from *offset_ptr.
LLVM_ABI uint8_t getU8(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint8_t value from *offset_ptr.
LLVM_ABI uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err=nullptr) const
Extract a unsigned LEB128 value from *offset_ptr.
void setAttributeString(unsigned tag, StringRef value)
Error integerAttribute(unsigned tag)
void parseIndexList(SmallVectorImpl< uint8_t > &indexList)
Error stringAttribute(unsigned tag)
Error parseAttributeList(uint32_t length)
void printAttribute(unsigned tag, unsigned value, StringRef valueDesc)
DataExtractor::Cursor cursor
Error parseSubsection(uint32_t length)
Error parse(ArrayRef< uint8_t > section, llvm::endianness endian) override
Error parseStringAttribute(const char *name, unsigned tag, ArrayRef< const char * > strings)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
static ErrorSuccess success()
Create a success value.
Definition: Error.h:336
virtual void printString(StringRef Value)
void indent(int Levels=1)
void unindent(int Levels=1)
void printEnum(StringRef Label, T Value, ArrayRef< EnumEntry< TEnum > > EnumValues)
virtual raw_ostream & startLine()
virtual void printNumber(StringRef Label, char Value)
void printList(StringRef Label, const ArrayRef< T > List)
bool empty() const
Definition: SmallVector.h:82
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:151
LLVM_ABI std::string lower() const
Definition: StringRef.cpp:112
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:418
LLVM_ABI StringRef attrTypeAsString(unsigned attr, TagNameMap tagNameMap, bool hasTagPrefix=true)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1702
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1305
endianness
Definition: bit.h:71
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1083