llvm.org GIT mirror llvm / 61afdad
[Object] Change getSectionName() to return Expected<StringRef> Summary: It currently receives an output parameter and returns std::error_code. Expected<StringRef> fits for this purpose perfectly. Differential Revision: https://reviews.llvm.org/D61421 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359774 91177308-0d34-0410-b5e6-96231b3b80d8 Fangrui Song 1 year, 5 months ago
17 changed file(s) with 90 addition(s) and 114 deletion(s). Raw diff Collapse all Expand all
896896 Expected getSymbolType(DataRefImpl Symb) const override;
897897 Expected getSymbolSection(DataRefImpl Symb) const override;
898898 void moveSectionNext(DataRefImpl &Sec) const override;
899 std::error_code getSectionName(DataRefImpl Sec,
900 StringRef &Res) const override;
899 Expected getSectionName(DataRefImpl Sec) const override;
901900 uint64_t getSectionAddress(DataRefImpl Sec) const override;
902901 uint64_t getSectionIndex(DataRefImpl Sec) const override;
903902 uint64_t getSectionSize(DataRefImpl Sec) const override;
10321031
10331032 ArrayRef getRelocations(const coff_section *Sec) const;
10341033
1035 std::error_code getSectionName(const coff_section *Sec, StringRef &Res) const;
1034 Expected getSectionName(const coff_section *Sec) const;
10361035 uint64_t getSectionSize(const coff_section *Sec) const;
10371036 std::error_code getSectionContents(const coff_section *Sec,
10381037 ArrayRef &Res) const;
258258 Expected getSymbolSection(DataRefImpl Symb) const override;
259259
260260 void moveSectionNext(DataRefImpl &Sec) const override;
261 std::error_code getSectionName(DataRefImpl Sec,
262 StringRef &Res) const override;
261 Expected getSectionName(DataRefImpl Sec) const override;
263262 uint64_t getSectionAddress(DataRefImpl Sec) const override;
264263 uint64_t getSectionIndex(DataRefImpl Sec) const override;
265264 uint64_t getSectionSize(DataRefImpl Sec) const override;
674673 }
675674
676675 template
677 std::error_code ELFObjectFile::getSectionName(DataRefImpl Sec,
678 StringRef &Result) const {
679 auto Name = EF.getSectionName(&*getSection(Sec));
680 if (!Name)
681 return errorToErrorCode(Name.takeError());
682 Result = *Name;
683 return std::error_code();
676 Expected ELFObjectFile::getSectionName(DataRefImpl Sec) const {
677 return EF.getSectionName(&*getSection(Sec));
684678 }
685679
686680 template
292292 unsigned getSectionID(SectionRef Sec) const;
293293
294294 void moveSectionNext(DataRefImpl &Sec) const override;
295 std::error_code getSectionName(DataRefImpl Sec,
296 StringRef &Res) const override;
295 Expected getSectionName(DataRefImpl Sec) const override;
297296 uint64_t getSectionAddress(DataRefImpl Sec) const override;
298297 uint64_t getSectionIndex(DataRefImpl Sec) const override;
299298 uint64_t getSectionSize(DataRefImpl Sec) const override;
256256 friend class SectionRef;
257257
258258 virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
259 virtual std::error_code getSectionName(DataRefImpl Sec,
260 StringRef &Res) const = 0;
259 virtual Expected getSectionName(DataRefImpl Sec) const = 0;
261260 virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
262261 virtual uint64_t getSectionIndex(DataRefImpl Sec) const = 0;
263262 virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
437436 }
438437
439438 inline std::error_code SectionRef::getName(StringRef &Result) const {
440 return OwningObject->getSectionName(SectionPimpl, Result);
439 Expected NameOrErr = OwningObject->getSectionName(SectionPimpl);
440 if (!NameOrErr)
441 return errorToErrorCode(NameOrErr.takeError());
442 Result = *NameOrErr;
443 return std::error_code();
441444 }
442445
443446 inline uint64_t SectionRef::getAddress() const {
170170
171171 // Overrides from SectionRef.
172172 void moveSectionNext(DataRefImpl &Sec) const override;
173 std::error_code getSectionName(DataRefImpl Sec,
174 StringRef &Res) const override;
173 Expected getSectionName(DataRefImpl Sec) const override;
175174 uint64_t getSectionAddress(DataRefImpl Sec) const override;
176175 uint64_t getSectionIndex(DataRefImpl Sec) const override;
177176 uint64_t getSectionSize(DataRefImpl Sec) const override;
8686 Expected getSymbolSection(DataRefImpl Symb) const override;
8787
8888 void moveSectionNext(DataRefImpl &Sec) const override;
89 std::error_code getSectionName(DataRefImpl Sec,
90 StringRef &Res) const override;
89 Expected getSectionName(DataRefImpl Sec) const override;
9190 uint64_t getSectionAddress(DataRefImpl Sec) const override;
9291 uint64_t getSectionIndex(DataRefImpl Sec) const override;
9392 uint64_t getSectionSize(DataRefImpl Sec) const override;
268268 Ref.p = reinterpret_cast(Sec);
269269 }
270270
271 std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
272 StringRef &Result) const {
271 Expected COFFObjectFile::getSectionName(DataRefImpl Ref) const {
273272 const coff_section *Sec = toSec(Ref);
274 return getSectionName(Sec, Result);
273 return getSectionName(Sec);
275274 }
276275
277276 uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
10731072 return Index;
10741073 }
10751074
1076 std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
1077 StringRef &Res) const {
1075 Expected
1076 COFFObjectFile::getSectionName(const coff_section *Sec) const {
10781077 StringRef Name;
10791078 if (Sec->Name[COFF::NameSize - 1] == 0)
10801079 // Null terminated, let ::strlen figure out the length.
10881087 uint32_t Offset;
10891088 if (Name.startswith("//")) {
10901089 if (decodeBase64StringEntry(Name.substr(2), Offset))
1091 return object_error::parse_failed;
1090 return createStringError(object_error::parse_failed,
1091 "inalid section name");
10921092 } else {
10931093 if (Name.substr(1).getAsInteger(10, Offset))
1094 return object_error::parse_failed;
1094 return createStringError(object_error::parse_failed,
1095 "invalid section name");
10951096 }
10961097 if (std::error_code EC = getString(Offset, Name))
1097 return EC;
1098 }
1099
1100 Res = Name;
1101 return std::error_code();
1098 return errorCodeToError(EC);
1099 }
1100
1101 return Name;
11021102 }
11031103
11041104 uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
18621862 Sec.d.a++;
18631863 }
18641864
1865 std::error_code MachOObjectFile::getSectionName(DataRefImpl Sec,
1866 StringRef &Result) const {
1865 Expected MachOObjectFile::getSectionName(DataRefImpl Sec) const {
18671866 ArrayRef Raw = getSectionRawName(Sec);
1868 Result = parseSegmentOrSectionName(Raw.data());
1869 return std::error_code();
1867 return parseSegmentOrSectionName(Raw.data());
18701868 }
18711869
18721870 uint64_t MachOObjectFile::getSectionAddress(DataRefImpl Sec) const {
19991997
20001998 bool MachOObjectFile::isSectionBitcode(DataRefImpl Sec) const {
20011999 StringRef SegmentName = getSectionFinalSegmentName(Sec);
2002 StringRef SectName;
2003 if (!getSectionName(Sec, SectName))
2004 return (SegmentName == "__LLVM" && SectName == "__bitcode");
2000 if (Expected NameOrErr = getSectionName(Sec))
2001 return (SegmentName == "__LLVM" && *NameOrErr == "__bitcode");
20052002 return false;
20062003 }
20072004
6767 uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
6868
6969 bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
70 StringRef SectName;
71 if (!getSectionName(Sec, SectName))
72 return SectName == ".llvmbc";
70 if (Expected NameOrErr = getSectionName(Sec))
71 return *NameOrErr == ".llvmbc";
7372 return false;
7473 }
7574
13881388
13891389 void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; }
13901390
1391 std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec,
1392 StringRef &Res) const {
1391 Expected WasmObjectFile::getSectionName(DataRefImpl Sec) const {
13931392 const WasmSection &S = Sections[Sec.d.a];
13941393 #define ECase(X) \
13951394 case wasm::WASM_SEC_##X: \
1396 Res = #X; \
1397 break
1395 return #X;
13981396 switch (S.Type) {
13991397 ECase(TYPE);
14001398 ECase(IMPORT);
14101408 ECase(DATA);
14111409 ECase(DATACOUNT);
14121410 case wasm::WASM_SEC_CUSTOM:
1413 Res = S.Name;
1414 break;
1411 return S.Name;
14151412 default:
1416 return object_error::invalid_section_index;
1413 return createStringError(object_error::invalid_section_index, "");
14171414 }
14181415 #undef ECase
1419 return std::error_code();
14201416 }
14211417
14221418 uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; }
118118 Sec.p = reinterpret_cast(Ptr + getSectionHeaderSize());
119119 }
120120
121 std::error_code XCOFFObjectFile::getSectionName(DataRefImpl Sec,
122 StringRef &Res) const {
121 Expected XCOFFObjectFile::getSectionName(DataRefImpl Sec) const {
123122 const char *Name = toSection(Sec)->Name;
124123 auto NulCharPtr =
125124 static_cast(memchr(Name, '\0', XCOFF::SectionNameSize));
126 Res = NulCharPtr ? StringRef(Name, NulCharPtr - Name)
127 : StringRef(Name, XCOFF::SectionNameSize);
128 return std::error_code();
125 return NulCharPtr ? StringRef(Name, NulCharPtr - Name)
126 : StringRef(Name, XCOFF::SectionNameSize);
129127 }
130128
131129 uint64_t XCOFFObjectFile::getSectionAddress(DataRefImpl Sec) const {
524524 }
525525 DataRefImpl Ref = Sec->getRawDataRefImpl();
526526 StringRef SectionName;
527 MachO->getSectionName(Ref, SectionName);
527 if (Expected NameOrErr = MachO->getSectionName(Ref))
528 SectionName = *NameOrErr;
528529 StringRef SegmentName = MachO->getSectionFinalSegmentName(Ref);
529530 outs() << "(" << SegmentName << "," << SectionName << ") ";
530531 break;
950951 section_iterator SecI = *SecIOrErr;
951952 const coff_section *Section = Obj.getCOFFSection(*SecI);
952953 Characteristics = Section->Characteristics;
953 StringRef SectionName;
954 Obj.getSectionName(Section, SectionName);
955 if (SectionName.startswith(".idata"))
956 return 'i';
954 if (Expected NameOrErr = Obj.getSectionName(Section))
955 if (NameOrErr->startswith(".idata"))
956 return 'i';
957957 }
958958
959959 switch (Symb.getSectionNumber()) {
10131013 return 's';
10141014 DataRefImpl Ref = Sec->getRawDataRefImpl();
10151015 StringRef SectionName;
1016 Obj.getSectionName(Ref, SectionName);
1016 if (Expected NameOrErr = Obj.getSectionName(Ref))
1017 SectionName = *NameOrErr;
10171018 StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
10181019 if (Obj.is64Bit() && Obj.getHeader64().filetype == MachO::MH_KEXT_BUNDLE &&
10191020 SegmentName == "__TEXT_EXEC" && SectionName == "__text")
11351136 for (auto &S : Obj->sections()) {
11361137 DataRefImpl Ref = S.getRawDataRefImpl();
11371138 StringRef SectionName;
1138 Obj->getSectionName(Ref, SectionName);
1139 if (Expected NameOrErr = Obj->getSectionName(Ref))
1140 SectionName = *NameOrErr;
11391141 StringRef SegmentName = Obj->getSectionFinalSegmentName(Ref);
11401142 if (SegmentName == SegSect[0] && SectionName == SegSect[1])
11411143 return Nsect;
7474 ArrayRef Relocs = COFFObj.getRelocations(Sec);
7575 for (const coff_relocation &R : Relocs)
7676 S.Relocs.push_back(R);
77 if (auto EC = COFFObj.getSectionName(Sec, S.Name))
78 return errorCodeToError(EC);
77 if (Expected NameOrErr = COFFObj.getSectionName(Sec))
78 S.Name = *NameOrErr;
79 else
80 return NameOrErr.takeError();
7981 if (Sec->hasExtendedRelocations())
8082 return createStringError(object_error::parse_failed,
8183 "Extended relocations not supported yet");
964964 object::DataRefImpl DRI;
965965 DRI.d.a = r_symbolnum-1;
966966 StringRef SegName = O->getSectionFinalSegmentName(DRI);
967 StringRef SectName;
968 if (O->getSectionName(DRI, SectName))
967 if (Expected NameOrErr = O->getSectionName(DRI))
968 outs() << "(" << SegName << "," << *NameOrErr << ")\n";
969 else
969970 outs() << "(?,?)\n";
970 else
971 outs() << "(" << SegName << "," << SectName << ")\n";
972971 }
973972 else {
974973 outs() << "(?,?)\n";
10211020 DataRefImpl DRI;
10221021 DRI.d.a = J;
10231022 const StringRef SegName = O->getSectionFinalSegmentName(DRI);
1024 StringRef SectName;
1025 if (O->getSectionName(DRI, SectName))
1023 if (Expected NameOrErr = O->getSectionName(DRI))
1024 outs() << "Relocation information (" << SegName << "," << *NameOrErr
1025 << format(") %u entries", Sec.nreloc);
1026 else
10261027 outs() << "Relocation information (" << SegName << ",?) "
10271028 << format("%u entries", Sec.nreloc);
1028 else
1029 outs() << "Relocation information (" << SegName << ","
1030 << SectName << format(") %u entries", Sec.nreloc);
10311029 outs() << "\naddress pcrel length extern type scattered "
10321030 "symbolnum/value\n";
10331031 PrintRelocationEntries(O, O->section_rel_begin(DRI),
10421040 DataRefImpl DRI;
10431041 DRI.d.a = J;
10441042 const StringRef SegName = O->getSectionFinalSegmentName(DRI);
1045 StringRef SectName;
1046 if (O->getSectionName(DRI, SectName))
1043 if (Expected NameOrErr = O->getSectionName(DRI))
1044 outs() << "Relocation information (" << SegName << "," << *NameOrErr
1045 << format(") %u entries", Sec.nreloc);
1046 else
10471047 outs() << "Relocation information (" << SegName << ",?) "
10481048 << format("%u entries", Sec.nreloc);
1049 else
1050 outs() << "Relocation information (" << SegName << ","
1051 << SectName << format(") %u entries", Sec.nreloc);
10521049 outs() << "\naddress pcrel length extern type scattered "
10531050 "symbolnum/value\n";
10541051 PrintRelocationEntries(O, O->section_rel_begin(DRI),
10931093 break;
10941094 }
10951095
1096 std::error_code Decoder::dumpProcedureData(const COFFObjectFile &COFF) {
1096 Error Decoder::dumpProcedureData(const COFFObjectFile &COFF) {
10971097 for (const auto &Section : COFF.sections()) {
1098 StringRef SectionName;
1099 if (std::error_code EC =
1100 COFF.getSectionName(COFF.getCOFFSection(Section), SectionName))
1101 return EC;
1102
1103 if (SectionName.startswith(".pdata"))
1098 Expected NameOrErr =
1099 COFF.getSectionName(COFF.getCOFFSection(Section));
1100 if (!NameOrErr)
1101 return NameOrErr.takeError();
1102
1103 if (NameOrErr->startswith(".pdata"))
11041104 dumpProcedureData(COFF, Section);
11051105 }
1106 return std::error_code();
1107 }
1108 }
1109 }
1110 }
1106 return Error::success();
1107 }
1108 }
1109 }
1110 }
155155 Decoder(ScopedPrinter &SW, bool isAArch64) : SW(SW),
156156 OS(SW.getOStream()),
157157 isAArch64(isAArch64) {}
158 std::error_code dumpProcedureData(const object::COFFObjectFile &COFF);
158 Error dumpProcedureData(const object::COFFObjectFile &COFF);
159159 };
160160 }
161161 }
13891389
13901390 void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); }
13911391
1392 static ErrorOr
1392 static Expected
13931393 getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber,
13941394 const coff_section *Section) {
1395 if (Section) {
1396 StringRef SectionName;
1397 if (std::error_code EC = Obj->getSectionName(Section, SectionName))
1398 return EC;
1399 return SectionName;
1400 }
1395 if (Section)
1396 return Obj->getSectionName(Section);
14011397 if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
14021398 return StringRef("IMAGE_SYM_DEBUG");
14031399 if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE)
14221418 if (Obj->getSymbolName(Symbol, SymbolName))
14231419 SymbolName = "";
14241420
1425 StringRef SectionName = "";
1426 ErrorOr Res =
1427 getSectionName(Obj, Symbol.getSectionNumber(), Section);
1428 if (Res)
1429 SectionName = *Res;
1421 StringRef SectionName;
1422 if (Expected NameOrErr =
1423 getSectionName(Obj, Symbol.getSectionNumber(), Section))
1424 SectionName = *NameOrErr;
14301425
14311426 W.printString("Name", SymbolName);
14321427 W.printNumber("Value", Symbol.getValue());
14941489 && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
14951490 const coff_section *Assoc;
14961491 StringRef AssocName = "";
1497 std::error_code EC = Obj->getSection(AuxNumber, Assoc);
1498 ErrorOr Res = getSectionName(Obj, AuxNumber, Assoc);
1499 if (Res)
1500 AssocName = *Res;
1501 if (!EC)
1502 EC = Res.getError();
1503 if (EC) {
1504 AssocName = "";
1492 if (std::error_code EC = Obj->getSection(AuxNumber, Assoc))
15051493 error(EC);
1506 }
1494 Expected Res = getSectionName(Obj, AuxNumber, Assoc);
1495 if (!Res)
1496 error(Res.takeError());
1497 AssocName = *Res;
15071498
15081499 W.printNumber("AssocSection", AssocName, AuxNumber);
15091500 }
15501541 case COFF::IMAGE_FILE_MACHINE_ARMNT: {
15511542 ARM::WinEH::Decoder Decoder(W, Obj->getMachine() ==
15521543 COFF::IMAGE_FILE_MACHINE_ARM64);
1553 Decoder.dumpProcedureData(*Obj);
1544 // TODO Propagate the error.
1545 consumeError(Decoder.dumpProcedureData(*Obj));
15541546 break;
15551547 }
15561548 default: