xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/Support/ELFAttributeParser.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
1*82d56013Sjoerg //===--- ELFAttributeParser.cpp - ELF Attribute Parser --------------------===//
2*82d56013Sjoerg //
3*82d56013Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*82d56013Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*82d56013Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*82d56013Sjoerg //
7*82d56013Sjoerg //===----------------------------------------------------------------------===//
8*82d56013Sjoerg 
9*82d56013Sjoerg #include "llvm/Support/ELFAttributeParser.h"
10*82d56013Sjoerg #include "llvm/ADT/STLExtras.h"
11*82d56013Sjoerg #include "llvm/ADT/StringExtras.h"
12*82d56013Sjoerg #include "llvm/Support/Errc.h"
13*82d56013Sjoerg #include "llvm/Support/LEB128.h"
14*82d56013Sjoerg #include "llvm/Support/ScopedPrinter.h"
15*82d56013Sjoerg 
16*82d56013Sjoerg using namespace llvm;
17*82d56013Sjoerg using namespace llvm::ELFAttrs;
18*82d56013Sjoerg 
19*82d56013Sjoerg static const EnumEntry<unsigned> tagNames[] = {
20*82d56013Sjoerg     {"Tag_File", ELFAttrs::File},
21*82d56013Sjoerg     {"Tag_Section", ELFAttrs::Section},
22*82d56013Sjoerg     {"Tag_Symbol", ELFAttrs::Symbol},
23*82d56013Sjoerg };
24*82d56013Sjoerg 
parseStringAttribute(const char * name,unsigned tag,ArrayRef<const char * > strings)25*82d56013Sjoerg Error ELFAttributeParser::parseStringAttribute(const char *name, unsigned tag,
26*82d56013Sjoerg                                                ArrayRef<const char *> strings) {
27*82d56013Sjoerg   uint64_t value = de.getULEB128(cursor);
28*82d56013Sjoerg   if (value >= strings.size()) {
29*82d56013Sjoerg     printAttribute(tag, value, "");
30*82d56013Sjoerg     return createStringError(errc::invalid_argument,
31*82d56013Sjoerg                              "unknown " + Twine(name) +
32*82d56013Sjoerg                                  " value: " + Twine(value));
33*82d56013Sjoerg   }
34*82d56013Sjoerg   printAttribute(tag, value, strings[value]);
35*82d56013Sjoerg   return Error::success();
36*82d56013Sjoerg }
37*82d56013Sjoerg 
integerAttribute(unsigned tag)38*82d56013Sjoerg Error ELFAttributeParser::integerAttribute(unsigned tag) {
39*82d56013Sjoerg   StringRef tagName =
40*82d56013Sjoerg       ELFAttrs::attrTypeAsString(tag, tagToStringMap, /*hasTagPrefix=*/false);
41*82d56013Sjoerg   uint64_t value = de.getULEB128(cursor);
42*82d56013Sjoerg   attributes.insert(std::make_pair(tag, value));
43*82d56013Sjoerg 
44*82d56013Sjoerg   if (sw) {
45*82d56013Sjoerg     DictScope scope(*sw, "Attribute");
46*82d56013Sjoerg     sw->printNumber("Tag", tag);
47*82d56013Sjoerg     if (!tagName.empty())
48*82d56013Sjoerg       sw->printString("TagName", tagName);
49*82d56013Sjoerg     sw->printNumber("Value", value);
50*82d56013Sjoerg   }
51*82d56013Sjoerg   return Error::success();
52*82d56013Sjoerg }
53*82d56013Sjoerg 
stringAttribute(unsigned tag)54*82d56013Sjoerg Error ELFAttributeParser::stringAttribute(unsigned tag) {
55*82d56013Sjoerg   StringRef tagName =
56*82d56013Sjoerg       ELFAttrs::attrTypeAsString(tag, tagToStringMap, /*hasTagPrefix=*/false);
57*82d56013Sjoerg   StringRef desc = de.getCStrRef(cursor);
58*82d56013Sjoerg   attributesStr.insert(std::make_pair(tag, desc));
59*82d56013Sjoerg 
60*82d56013Sjoerg   if (sw) {
61*82d56013Sjoerg     DictScope scope(*sw, "Attribute");
62*82d56013Sjoerg     sw->printNumber("Tag", tag);
63*82d56013Sjoerg     if (!tagName.empty())
64*82d56013Sjoerg       sw->printString("TagName", tagName);
65*82d56013Sjoerg     sw->printString("Value", desc);
66*82d56013Sjoerg   }
67*82d56013Sjoerg   return Error::success();
68*82d56013Sjoerg }
69*82d56013Sjoerg 
printAttribute(unsigned tag,unsigned value,StringRef valueDesc)70*82d56013Sjoerg void ELFAttributeParser::printAttribute(unsigned tag, unsigned value,
71*82d56013Sjoerg                                         StringRef valueDesc) {
72*82d56013Sjoerg   attributes.insert(std::make_pair(tag, value));
73*82d56013Sjoerg 
74*82d56013Sjoerg   if (sw) {
75*82d56013Sjoerg     StringRef tagName = ELFAttrs::attrTypeAsString(tag, tagToStringMap,
76*82d56013Sjoerg                                                    /*hasTagPrefix=*/false);
77*82d56013Sjoerg     DictScope as(*sw, "Attribute");
78*82d56013Sjoerg     sw->printNumber("Tag", tag);
79*82d56013Sjoerg     sw->printNumber("Value", value);
80*82d56013Sjoerg     if (!tagName.empty())
81*82d56013Sjoerg       sw->printString("TagName", tagName);
82*82d56013Sjoerg     if (!valueDesc.empty())
83*82d56013Sjoerg       sw->printString("Description", valueDesc);
84*82d56013Sjoerg   }
85*82d56013Sjoerg }
86*82d56013Sjoerg 
parseIndexList(SmallVectorImpl<uint8_t> & indexList)87*82d56013Sjoerg void ELFAttributeParser::parseIndexList(SmallVectorImpl<uint8_t> &indexList) {
88*82d56013Sjoerg   for (;;) {
89*82d56013Sjoerg     uint64_t value = de.getULEB128(cursor);
90*82d56013Sjoerg     if (!cursor || !value)
91*82d56013Sjoerg       break;
92*82d56013Sjoerg     indexList.push_back(value);
93*82d56013Sjoerg   }
94*82d56013Sjoerg }
95*82d56013Sjoerg 
parseAttributeList(uint32_t length)96*82d56013Sjoerg Error ELFAttributeParser::parseAttributeList(uint32_t length) {
97*82d56013Sjoerg   uint64_t pos;
98*82d56013Sjoerg   uint64_t end = cursor.tell() + length;
99*82d56013Sjoerg   while ((pos = cursor.tell()) < end) {
100*82d56013Sjoerg     uint64_t tag = de.getULEB128(cursor);
101*82d56013Sjoerg     bool handled;
102*82d56013Sjoerg     if (Error e = handler(tag, handled))
103*82d56013Sjoerg       return e;
104*82d56013Sjoerg 
105*82d56013Sjoerg     if (!handled) {
106*82d56013Sjoerg       if (tag < 32) {
107*82d56013Sjoerg         return createStringError(errc::invalid_argument,
108*82d56013Sjoerg                                  "invalid tag 0x" + Twine::utohexstr(tag) +
109*82d56013Sjoerg                                      " at offset 0x" + Twine::utohexstr(pos));
110*82d56013Sjoerg       }
111*82d56013Sjoerg 
112*82d56013Sjoerg       if (tag % 2 == 0) {
113*82d56013Sjoerg         if (Error e = integerAttribute(tag))
114*82d56013Sjoerg           return e;
115*82d56013Sjoerg       } else {
116*82d56013Sjoerg         if (Error e = stringAttribute(tag))
117*82d56013Sjoerg           return e;
118*82d56013Sjoerg       }
119*82d56013Sjoerg     }
120*82d56013Sjoerg   }
121*82d56013Sjoerg   return Error::success();
122*82d56013Sjoerg }
123*82d56013Sjoerg 
parseSubsection(uint32_t length)124*82d56013Sjoerg Error ELFAttributeParser::parseSubsection(uint32_t length) {
125*82d56013Sjoerg   uint64_t end = cursor.tell() - sizeof(length) + length;
126*82d56013Sjoerg   StringRef vendorName = de.getCStrRef(cursor);
127*82d56013Sjoerg   if (sw) {
128*82d56013Sjoerg     sw->printNumber("SectionLength", length);
129*82d56013Sjoerg     sw->printString("Vendor", vendorName);
130*82d56013Sjoerg   }
131*82d56013Sjoerg 
132*82d56013Sjoerg   // Ignore unrecognized vendor-name.
133*82d56013Sjoerg   if (vendorName.lower() != vendor)
134*82d56013Sjoerg     return createStringError(errc::invalid_argument,
135*82d56013Sjoerg                              "unrecognized vendor-name: " + vendorName);
136*82d56013Sjoerg 
137*82d56013Sjoerg   while (cursor.tell() < end) {
138*82d56013Sjoerg     /// Tag_File | Tag_Section | Tag_Symbol   uleb128:byte-size
139*82d56013Sjoerg     uint8_t tag = de.getU8(cursor);
140*82d56013Sjoerg     uint32_t size = de.getU32(cursor);
141*82d56013Sjoerg     if (!cursor)
142*82d56013Sjoerg       return cursor.takeError();
143*82d56013Sjoerg 
144*82d56013Sjoerg     if (sw) {
145*82d56013Sjoerg       sw->printEnum("Tag", tag, makeArrayRef(tagNames));
146*82d56013Sjoerg       sw->printNumber("Size", size);
147*82d56013Sjoerg     }
148*82d56013Sjoerg     if (size < 5)
149*82d56013Sjoerg       return createStringError(errc::invalid_argument,
150*82d56013Sjoerg                                "invalid attribute size " + Twine(size) +
151*82d56013Sjoerg                                    " at offset 0x" +
152*82d56013Sjoerg                                    Twine::utohexstr(cursor.tell() - 5));
153*82d56013Sjoerg 
154*82d56013Sjoerg     StringRef scopeName, indexName;
155*82d56013Sjoerg     SmallVector<uint8_t, 8> indicies;
156*82d56013Sjoerg     switch (tag) {
157*82d56013Sjoerg     case ELFAttrs::File:
158*82d56013Sjoerg       scopeName = "FileAttributes";
159*82d56013Sjoerg       break;
160*82d56013Sjoerg     case ELFAttrs::Section:
161*82d56013Sjoerg       scopeName = "SectionAttributes";
162*82d56013Sjoerg       indexName = "Sections";
163*82d56013Sjoerg       parseIndexList(indicies);
164*82d56013Sjoerg       break;
165*82d56013Sjoerg     case ELFAttrs::Symbol:
166*82d56013Sjoerg       scopeName = "SymbolAttributes";
167*82d56013Sjoerg       indexName = "Symbols";
168*82d56013Sjoerg       parseIndexList(indicies);
169*82d56013Sjoerg       break;
170*82d56013Sjoerg     default:
171*82d56013Sjoerg       return createStringError(errc::invalid_argument,
172*82d56013Sjoerg                                "unrecognized tag 0x" + Twine::utohexstr(tag) +
173*82d56013Sjoerg                                    " at offset 0x" +
174*82d56013Sjoerg                                    Twine::utohexstr(cursor.tell() - 5));
175*82d56013Sjoerg     }
176*82d56013Sjoerg 
177*82d56013Sjoerg     if (sw) {
178*82d56013Sjoerg       DictScope scope(*sw, scopeName);
179*82d56013Sjoerg       if (!indicies.empty())
180*82d56013Sjoerg         sw->printList(indexName, indicies);
181*82d56013Sjoerg       if (Error e = parseAttributeList(size - 5))
182*82d56013Sjoerg         return e;
183*82d56013Sjoerg     } else if (Error e = parseAttributeList(size - 5))
184*82d56013Sjoerg       return e;
185*82d56013Sjoerg   }
186*82d56013Sjoerg   return Error::success();
187*82d56013Sjoerg }
188*82d56013Sjoerg 
parse(ArrayRef<uint8_t> section,support::endianness endian)189*82d56013Sjoerg Error ELFAttributeParser::parse(ArrayRef<uint8_t> section,
190*82d56013Sjoerg                                 support::endianness endian) {
191*82d56013Sjoerg   unsigned sectionNumber = 0;
192*82d56013Sjoerg   de = DataExtractor(section, endian == support::little, 0);
193*82d56013Sjoerg 
194*82d56013Sjoerg   // For early returns, we have more specific errors, consume the Error in
195*82d56013Sjoerg   // cursor.
196*82d56013Sjoerg   struct ClearCursorError {
197*82d56013Sjoerg     DataExtractor::Cursor &cursor;
198*82d56013Sjoerg     ~ClearCursorError() { consumeError(cursor.takeError()); }
199*82d56013Sjoerg   } clear{cursor};
200*82d56013Sjoerg 
201*82d56013Sjoerg   // Unrecognized format-version.
202*82d56013Sjoerg   uint8_t formatVersion = de.getU8(cursor);
203*82d56013Sjoerg   if (formatVersion != ELFAttrs::Format_Version)
204*82d56013Sjoerg     return createStringError(errc::invalid_argument,
205*82d56013Sjoerg                              "unrecognized format-version: 0x" +
206*82d56013Sjoerg                                  utohexstr(formatVersion));
207*82d56013Sjoerg 
208*82d56013Sjoerg   while (!de.eof(cursor)) {
209*82d56013Sjoerg     uint32_t sectionLength = de.getU32(cursor);
210*82d56013Sjoerg     if (!cursor)
211*82d56013Sjoerg       return cursor.takeError();
212*82d56013Sjoerg 
213*82d56013Sjoerg     if (sw) {
214*82d56013Sjoerg       sw->startLine() << "Section " << ++sectionNumber << " {\n";
215*82d56013Sjoerg       sw->indent();
216*82d56013Sjoerg     }
217*82d56013Sjoerg 
218*82d56013Sjoerg     if (sectionLength < 4 || cursor.tell() - 4 + sectionLength > section.size())
219*82d56013Sjoerg       return createStringError(errc::invalid_argument,
220*82d56013Sjoerg                                "invalid section length " +
221*82d56013Sjoerg                                    Twine(sectionLength) + " at offset 0x" +
222*82d56013Sjoerg                                    utohexstr(cursor.tell() - 4));
223*82d56013Sjoerg 
224*82d56013Sjoerg     if (Error e = parseSubsection(sectionLength))
225*82d56013Sjoerg       return e;
226*82d56013Sjoerg     if (sw) {
227*82d56013Sjoerg       sw->unindent();
228*82d56013Sjoerg       sw->startLine() << "}\n";
229*82d56013Sjoerg     }
230*82d56013Sjoerg   }
231*82d56013Sjoerg 
232*82d56013Sjoerg   return cursor.takeError();
233*82d56013Sjoerg }
234