llvm.org GIT mirror llvm / ff67629
Don't return error_code from function that never fails. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241021 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
23 changed file(s) with 49 addition(s) and 106 deletion(s). Raw diff Collapse all Expand all
660660 void moveRelocationNext(DataRefImpl &Rel) const override;
661661 std::error_code getRelocationAddress(DataRefImpl Rel,
662662 uint64_t &Res) const override;
663 std::error_code getRelocationOffset(DataRefImpl Rel,
664 uint64_t &Res) const override;
663 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
665664 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
666665 std::error_code getRelocationType(DataRefImpl Rel,
667666 uint64_t &Res) const override;
200200 void moveRelocationNext(DataRefImpl &Rel) const override;
201201 std::error_code getRelocationAddress(DataRefImpl Rel,
202202 uint64_t &Res) const override;
203 std::error_code getRelocationOffset(DataRefImpl Rel,
204 uint64_t &Res) const override;
203 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
205204 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
206205 std::error_code getRelocationType(DataRefImpl Rel,
207206 uint64_t &Res) const override;
688687 }
689688
690689 template
691 std::error_code
692 ELFObjectFile::getRelocationOffset(DataRefImpl Rel,
693 uint64_t &Result) const {
690 uint64_t ELFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
694691 assert(EF.getHeader()->e_type == ELF::ET_REL &&
695692 "Only relocatable object files have relocation offsets");
696 Result = getROffset(Rel);
697 return std::error_code();
693 return getROffset(Rel);
698694 }
699695
700696 template
236236 void moveRelocationNext(DataRefImpl &Rel) const override;
237237 std::error_code getRelocationAddress(DataRefImpl Rel,
238238 uint64_t &Res) const override;
239 std::error_code getRelocationOffset(DataRefImpl Rel,
240 uint64_t &Res) const override;
239 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
241240 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
242241 section_iterator getRelocationSection(DataRefImpl Rel) const;
243242 std::error_code getRelocationType(DataRefImpl Rel,
5050 void moveNext();
5151
5252 std::error_code getAddress(uint64_t &Result) const;
53 std::error_code getOffset(uint64_t &Result) const;
53 uint64_t getOffset() const;
5454 symbol_iterator getSymbol() const;
5555 std::error_code getType(uint64_t &Result) const;
5656
239239 virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
240240 virtual std::error_code getRelocationAddress(DataRefImpl Rel,
241241 uint64_t &Res) const = 0;
242 virtual std::error_code getRelocationOffset(DataRefImpl Rel,
243 uint64_t &Res) const = 0;
242 virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
244243 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
245244 virtual std::error_code getRelocationType(DataRefImpl Rel,
246245 uint64_t &Res) const = 0;
456455 return OwningObject->getRelocationAddress(RelocationPimpl, Result);
457456 }
458457
459 inline std::error_code RelocationRef::getOffset(uint64_t &Result) const {
460 return OwningObject->getRelocationOffset(RelocationPimpl, Result);
458 inline uint64_t RelocationRef::getOffset() const {
459 return OwningObject->getRelocationOffset(RelocationPimpl);
461460 }
462461
463462 inline symbol_iterator RelocationRef::getSymbol() const {
266266 }
267267
268268 RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value) {
269 uint64_t Address;
270 R.getOffset(Address);
269 uint64_t Address = R.getOffset();
271270 return RelocToApply(Value - Address, 4);
272271 }
273272
281280 }
282281 RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value) {
283282 int64_t Addend = getELFAddend(R);
284 uint64_t Address;
285 R.getOffset(Address);
283 uint64_t Address = R.getOffset();
286284 return RelocToApply(Value + Addend - Address, 4);
287285 }
288286 RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
666666 if (Section.relocation_begin() != Section.relocation_end()) {
667667 uint64_t SectionSize = RelocatedSection->getSize();
668668 for (const RelocationRef &Reloc : Section.relocations()) {
669 uint64_t Address;
670 Reloc.getOffset(Address);
669 uint64_t Address = Reloc.getOffset();
671670 uint64_t Type;
672671 Reloc.getType(Type);
673672 uint64_t SymAddr = 0;
778778 continue;
779779 }
780780
781 uint64_t TargetSymbolOffset;
781 uint64_t TargetSymbolOffset = i->getOffset();
782782 symbol_iterator TargetSymbol = i->getSymbol();
783 check(i->getOffset(TargetSymbolOffset));
784783 ErrorOr AddendOrErr =
785784 Obj.getRelocationAddend(i->getRawDataRefImpl());
786785 Check(AddendOrErr.getError());
11231122 }
11241123 }
11251124
1126 uint64_t Offset;
1127 Check(RelI->getOffset(Offset));
1125 uint64_t Offset = RelI->getOffset();
11281126
11291127 DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
11301128 << "\n");
7171
7272 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
7373 unsigned Size = Obj.getAnyRelocationLength(RelInfo);
74 uint64_t Offset;
75 RI->getOffset(Offset);
74 uint64_t Offset = RI->getOffset();
7675 MachO::RelocationInfoType RelType =
7776 static_cast(Obj.getAnyRelocationType(RelInfo));
7877
126126 // Determine the Addend used to adjust the relocation value.
127127 uint64_t RelType;
128128 Check(RelI->getType(RelType));
129 uint64_t Offset;
130 Check(RelI->getOffset(Offset));
129 uint64_t Offset = RelI->getOffset();
131130 uint64_t Addend = 0;
132131 SectionEntry &Section = Sections[SectionID];
133132 uintptr_t ObjTarget = Section.ObjAddress + Offset;
219219 SectionEntry &Section = Sections[SectionID];
220220 uint32_t RelocType = MachO.getAnyRelocationType(RE);
221221 bool IsPCRel = MachO.getAnyRelocationPCRel(RE);
222 uint64_t Offset;
223 RelI->getOffset(Offset);
222 uint64_t Offset = RelI->getOffset();
224223 uint8_t *LocalAddress = Section.Address + Offset;
225224 int64_t Immediate = readBytesUnaligned(LocalAddress, 4); // Copy the whole instruction out.
226225 Immediate = ((Immediate >> 4) & 0xf000) | (Immediate & 0xfff);
137137 uint32_t RelocType = Obj.getAnyRelocationType(RE);
138138 bool IsPCRel = Obj.getAnyRelocationPCRel(RE);
139139 unsigned Size = Obj.getAnyRelocationLength(RE);
140 uint64_t Offset;
141 RelI->getOffset(Offset);
140 uint64_t Offset = RelI->getOffset();
142141 uint8_t *LocalAddress = Section.Address + Offset;
143142 unsigned NumBytes = 1 << Size;
144143 uint64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
196195 uint32_t RelocType = Obj.getAnyRelocationType(RE);
197196 bool IsPCRel = Obj.getAnyRelocationPCRel(RE);
198197 unsigned Size = Obj.getAnyRelocationLength(RE);
199 uint64_t Offset;
200 RelI->getOffset(Offset);
198 uint64_t Offset = RelI->getOffset();
201199 uint8_t *LocalAddress = Section.Address + Offset;
202200 unsigned NumBytes = 1 << Size;
203201 int64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
966966 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
967967 }
968968
969 std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
970 uint64_t &Res) const {
969 uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
971970 const coff_relocation *R = toRel(Rel);
972 const support::ulittle32_t *VirtualAddressPtr;
973 if (std::error_code EC =
974 getObject(VirtualAddressPtr, Data, &R->VirtualAddress))
975 return EC;
976 Res = *VirtualAddressPtr;
977 return std::error_code();
971 return R->VirtualAddress;
978972 }
979973
980974 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
611611
612612 std::error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
613613 uint64_t &Res) const {
614 uint64_t Offset;
615 getRelocationOffset(Rel, Offset);
614 uint64_t Offset = getRelocationOffset(Rel);
616615
617616 DataRefImpl Sec;
618617 Sec.d.a = Rel.d.a;
621620 return std::error_code();
622621 }
623622
624 std::error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
625 uint64_t &Res) const {
623 uint64_t MachOObjectFile::getRelocationOffset(DataRefImpl Rel) const {
626624 assert(getHeader().filetype == MachO::MH_OBJECT &&
627625 "Only implemented for MH_OBJECT");
628626 MachO::any_relocation_info RE = getRelocation(Rel);
629 Res = getAnyRelocationAddress(RE);
630 return std::error_code();
627 return getAnyRelocationAddress(RE);
631628 }
632629
633630 symbol_iterator
198198 }
199199
200200 uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
201 uint64_t ret;
202 if (std::error_code ec = (*unwrap(RI))->getOffset(ret))
203 report_fatal_error(ec.message());
204 return ret;
201 return (*unwrap(RI))->getOffset();
205202 }
206203
207204 LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
14501450 object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
14511451 MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
14521452 unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
1453 uint64_t Offset64;
1454 if ((RelocSize != 4 && RelocSize != 8) || Reloc.getOffset(Offset64)) {
1453 uint64_t Offset64 = Reloc.getOffset();
1454 if ((RelocSize != 4 && RelocSize != 8)) {
14551455 reportWarning(" unsupported relocation in debug_info section.");
14561456 continue;
14571457 }
9999 StringRef RelocSymName;
100100 if (error(RelocSymI->getName(RelocSymName)))
101101 return true;
102 uint64_t Offset;
103 if (error(Reloc.getOffset(Offset)))
104 return true;
102 uint64_t Offset = Reloc.getOffset();
105103 if (Offset >= SymOffset && Offset < SymEnd) {
106104 *I = RelocSymName;
107105 ++I;
125123 StringRef RelocSymName;
126124 if (error(RelocSymI->getName(RelocSymName)))
127125 return true;
128 uint64_t Offset;
129 if (error(Reloc.getOffset(Offset)))
130 return true;
126 uint64_t Offset = Reloc.getOffset();
131127 if (Offset >= SymOffset && Offset < SymEnd)
132128 Collection[std::make_pair(SymName, Offset - SymOffset)] = RelocSymName;
133129 }
176176 for (std::vector::const_iterator I = Rels.begin(),
177177 E = Rels.end();
178178 I != E; ++I) {
179 uint64_t Ofs;
180 if (std::error_code EC = I->getOffset(Ofs))
181 return EC;
179 uint64_t Ofs = I->getOffset();
182180 if (Ofs == Offset) {
183181 Sym = *I->getSymbol();
184182 return std::error_code();
795795 RE = O->getRelocation(Rel);
796796 isExtern = O->getPlainRelocationExternal(RE);
797797 if (isExtern) {
798 uint64_t RelocOffset;
799 Reloc.getOffset(RelocOffset);
798 uint64_t RelocOffset = Reloc.getOffset();
800799 symbol_iterator RelocSym = Reloc.getSymbol();
801800 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
802801 }
17621761 bool r_scattered = false;
17631762 uint32_t r_value, pair_r_value, r_type;
17641763 for (const RelocationRef &Reloc : info->S.relocations()) {
1765 uint64_t RelocOffset;
1766 Reloc.getOffset(RelocOffset);
1764 uint64_t RelocOffset = Reloc.getOffset();
17671765 if (RelocOffset == sect_offset) {
17681766 Rel = Reloc.getRawDataRefImpl();
17691767 RE = info->O->getRelocation(Rel);
18401838 bool isExtern = false;
18411839 SymbolRef Symbol;
18421840 for (const RelocationRef &Reloc : info->S.relocations()) {
1843 uint64_t RelocOffset;
1844 Reloc.getOffset(RelocOffset);
1841 uint64_t RelocOffset = Reloc.getOffset();
18451842 if (RelocOffset == sect_offset) {
18461843 Rel = Reloc.getRawDataRefImpl();
18471844 RE = info->O->getRelocation(Rel);
19101907 auto Reloc =
19111908 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
19121909 [&](const RelocationRef &Reloc) {
1913 uint64_t RelocOffset;
1914 Reloc.getOffset(RelocOffset);
1910 uint64_t RelocOffset = Reloc.getOffset();
19151911 return RelocOffset == sect_offset;
19161912 });
19171913
20372033 auto Reloc =
20382034 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
20392035 [&](const RelocationRef &Reloc) {
2040 uint64_t RelocOffset;
2041 Reloc.getOffset(RelocOffset);
2036 uint64_t RelocOffset = Reloc.getOffset();
20422037 return RelocOffset == sect_offset;
20432038 });
20442039
24282423 bool isExtern = false;
24292424 SymbolRef Symbol;
24302425 for (const RelocationRef &Reloc : S.relocations()) {
2431 uint64_t RelocOffset;
2432 Reloc.getOffset(RelocOffset);
2426 uint64_t RelocOffset = Reloc.getOffset();
24332427 if (RelocOffset == sect_offset) {
24342428 Rel = Reloc.getRawDataRefImpl();
24352429 RE = info->O->getRelocation(Rel);
56105604 bool isExtern = false;
56115605 SymbolRef Symbol;
56125606 for (const RelocationRef &Reloc : info->S.relocations()) {
5613 uint64_t RelocOffset;
5614 Reloc.getOffset(RelocOffset);
5607 uint64_t RelocOffset = Reloc.getOffset();
56155608 if (RelocOffset == sect_offset) {
56165609 Rel = Reloc.getRawDataRefImpl();
56175610 RE = info->O->getRelocation(Rel);
61056098 // Parse relocations.
61066099 std::vector> Relocs;
61076100 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
6108 uint64_t RelocOffset;
6109 Reloc.getOffset(RelocOffset);
6101 uint64_t RelocOffset = Reloc.getOffset();
61106102 uint64_t SectionAddress = Sections[SectIdx].getAddress();
61116103 RelocOffset -= SectionAddress;
61126104
65096501 // Next we need to look at the relocations to find out what objects are
65106502 // actually being referred to.
65116503 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
6512 uint64_t RelocAddress;
6513 Reloc.getOffset(RelocAddress);
6504 uint64_t RelocAddress = Reloc.getOffset();
65146505
65156506 uint32_t EntryIdx = RelocAddress / EntrySize;
65166507 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
211211 }
212212
213213 bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
214 uint64_t a_addr, b_addr;
215 if (error(a.getOffset(a_addr))) return false;
216 if (error(b.getOffset(b_addr))) return false;
214 uint64_t a_addr = a.getOffset();
215 uint64_t b_addr = b.getOffset();
217216 return a_addr < b_addr;
218217 }
219218
889888 // Print relocation for instruction.
890889 while (rel_cur != rel_end) {
891890 bool hidden = false;
892 uint64_t addr;
891 uint64_t addr = rel_cur->getOffset();
893892 SmallString<16> name;
894893 SmallString<32> val;
895894
897896 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
898897 if (hidden) goto skip_print_rel;
899898
900 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
901899 // Stop when rel_cur's address is past the current instruction.
902900 if (addr >= Index + Size) break;
903901 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
931929 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
932930 for (const RelocationRef &Reloc : Section.relocations()) {
933931 bool hidden;
934 uint64_t address;
932 uint64_t address = Reloc.getOffset();
935933 SmallString<32> relocname;
936934 SmallString<32> valuestr;
937935 if (error(Reloc.getHidden(hidden)))
939937 if (hidden)
940938 continue;
941939 if (error(Reloc.getTypeName(relocname)))
942 continue;
943 if (error(Reloc.getOffset(address)))
944940 continue;
945941 if (error(getRelocationValueString(Reloc, valuestr)))
946942 continue;
213213 const SectionRef &Section,
214214 uint64_t Offset) {
215215 for (const auto &Relocation : Section.relocations()) {
216 uint64_t RelocationOffset;
217 if (auto Error = Relocation.getOffset(RelocationOffset))
218 return Error;
216 uint64_t RelocationOffset = Relocation.getOffset();
219217 if (RelocationOffset == Offset)
220218 return *Relocation.getSymbol();
221219 }
120120 uint64_t Offset, SymbolRef &Sym) {
121121 const auto &Relocations = RelocMap[Section];
122122 for (const auto &Relocation : Relocations) {
123 uint64_t RelocationOffset;
124 if (std::error_code EC = Relocation.getOffset(RelocationOffset))
125 return EC;
123 uint64_t RelocationOffset = Relocation.getOffset();
126124
127125 if (RelocationOffset == Offset) {
128126 Sym = *Relocation.getSymbol();
804802
805803 void COFFDumper::printRelocation(const SectionRef &Section,
806804 const RelocationRef &Reloc) {
807 uint64_t Offset;
805 uint64_t Offset = Reloc.getOffset();
808806 uint64_t RelocType;
809807 SmallString<32> RelocName;
810808 StringRef SymbolName;
811 if (error(Reloc.getOffset(Offset)))
812 return;
813809 if (error(Reloc.getType(RelocType)))
814810 return;
815811 if (error(Reloc.getTypeName(RelocName)))
460460
461461 void MachODumper::printRelocation(const MachOObjectFile *Obj,
462462 const RelocationRef &Reloc) {
463 uint64_t Offset;
463 uint64_t Offset = Reloc.getOffset();
464464 SmallString<32> RelocName;
465 if (error(Reloc.getOffset(Offset)))
466 return;
467465 if (error(Reloc.getTypeName(RelocName)))
468466 return;
469467
198198 }
199199
200200 bool relocAddressLess(RelocationRef a, RelocationRef b) {
201 uint64_t a_addr, b_addr;
202 if (error(a.getOffset(a_addr))) exit(ReturnValue);
203 if (error(b.getOffset(b_addr))) exit(ReturnValue);
201 uint64_t a_addr = a.getOffset();
202 uint64_t b_addr = b.getOffset();
204203 return a_addr < b_addr;
205204 }
206205