llvm.org GIT mirror llvm / 4e2b922
Remove 'using std::errro_code' from lib. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@210871 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
42 changed file(s) with 624 addition(s) and 642 deletion(s). Raw diff Collapse all Expand all
1919 #include
2020 #include
2121 using namespace llvm;
22 using std::error_code;
2322
2423 Module *llvm::ParseAssembly(MemoryBuffer *F,
2524 Module *M,
4241 Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
4342 LLVMContext &Context) {
4443 std::unique_ptr File;
45 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
44 if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
4645 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
4746 "Could not open input file: " + ec.message());
4847 return nullptr;
1515 #include
1616
1717 using namespace llvm;
18 using std::error_code;
1918
2019 /* Builds a module from the bitcode in the specified memory buffer, returning a
2120 reference to the module via the OutModule parameter. Returns 0 on success.
3231 char **OutMessage) {
3332 ErrorOr ModuleOrErr =
3433 parseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef));
35 if (error_code EC = ModuleOrErr.getError()) {
34 if (std::error_code EC = ModuleOrErr.getError()) {
3635 if (OutMessage)
3736 *OutMessage = strdup(EC.message().c_str());
3837 *OutModule = wrap((Module*)nullptr);
5453 ErrorOr ModuleOrErr =
5554 getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef));
5655
57 if (error_code EC = ModuleOrErr.getError()) {
56 if (std::error_code EC = ModuleOrErr.getError()) {
5857 *OutM = wrap((Module *)nullptr);
5958 if (OutMessage)
6059 *OutMessage = strdup(EC.message().c_str());
2525 #include "llvm/Support/MemoryBuffer.h"
2626 #include "llvm/Support/raw_ostream.h"
2727 using namespace llvm;
28 using std::error_code;
2928
3029 enum {
3130 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
470469 (EncodedAttrs & 0xffff));
471470 }
472471
473 error_code BitcodeReader::ParseAttributeBlock() {
472 std::error_code BitcodeReader::ParseAttributeBlock() {
474473 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
475474 return Error(InvalidRecord);
476475
490489 case BitstreamEntry::Error:
491490 return Error(MalformedBlock);
492491 case BitstreamEntry::EndBlock:
493 return error_code();
492 return std::error_code();
494493 case BitstreamEntry::Record:
495494 // The interesting case.
496495 break;
616615 }
617616 }
618617
619 error_code BitcodeReader::ParseAttrKind(uint64_t Code,
620 Attribute::AttrKind *Kind) {
618 std::error_code BitcodeReader::ParseAttrKind(uint64_t Code,
619 Attribute::AttrKind *Kind) {
621620 *Kind = GetAttrFromCode(Code);
622621 if (*Kind == Attribute::None)
623622 return Error(InvalidValue);
624 return error_code();
625 }
626
627 error_code BitcodeReader::ParseAttributeGroupBlock() {
623 return std::error_code();
624 }
625
626 std::error_code BitcodeReader::ParseAttributeGroupBlock() {
628627 if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
629628 return Error(InvalidRecord);
630629
642641 case BitstreamEntry::Error:
643642 return Error(MalformedBlock);
644643 case BitstreamEntry::EndBlock:
645 return error_code();
644 return std::error_code();
646645 case BitstreamEntry::Record:
647646 // The interesting case.
648647 break;
664663 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
665664 if (Record[i] == 0) { // Enum attribute
666665 Attribute::AttrKind Kind;
667 if (error_code EC = ParseAttrKind(Record[++i], &Kind))
666 if (std::error_code EC = ParseAttrKind(Record[++i], &Kind))
668667 return EC;
669668
670669 B.addAttribute(Kind);
671670 } else if (Record[i] == 1) { // Align attribute
672671 Attribute::AttrKind Kind;
673 if (error_code EC = ParseAttrKind(Record[++i], &Kind))
672 if (std::error_code EC = ParseAttrKind(Record[++i], &Kind))
674673 return EC;
675674 if (Kind == Attribute::Alignment)
676675 B.addAlignmentAttr(Record[++i]);
706705 }
707706 }
708707
709 error_code BitcodeReader::ParseTypeTable() {
708 std::error_code BitcodeReader::ParseTypeTable() {
710709 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
711710 return Error(InvalidRecord);
712711
713712 return ParseTypeTableBody();
714713 }
715714
716 error_code BitcodeReader::ParseTypeTableBody() {
715 std::error_code BitcodeReader::ParseTypeTableBody() {
717716 if (!TypeList.empty())
718717 return Error(InvalidMultipleBlocks);
719718
733732 case BitstreamEntry::EndBlock:
734733 if (NumRecords != TypeList.size())
735734 return Error(MalformedBlock);
736 return error_code();
735 return std::error_code();
737736 case BitstreamEntry::Record:
738737 // The interesting case.
739738 break;
933932 }
934933 }
935934
936 error_code BitcodeReader::ParseValueSymbolTable() {
935 std::error_code BitcodeReader::ParseValueSymbolTable() {
937936 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
938937 return Error(InvalidRecord);
939938
949948 case BitstreamEntry::Error:
950949 return Error(MalformedBlock);
951950 case BitstreamEntry::EndBlock:
952 return error_code();
951 return std::error_code();
953952 case BitstreamEntry::Record:
954953 // The interesting case.
955954 break;
987986 }
988987 }
989988
990 error_code BitcodeReader::ParseMetadata() {
989 std::error_code BitcodeReader::ParseMetadata() {
991990 unsigned NextMDValueNo = MDValueList.size();
992991
993992 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
10041003 case BitstreamEntry::Error:
10051004 return Error(MalformedBlock);
10061005 case BitstreamEntry::EndBlock:
1007 return error_code();
1006 return std::error_code();
10081007 case BitstreamEntry::Record:
10091008 // The interesting case.
10101009 break;
10981097
10991098 /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
11001099 /// values and aliases that we can.
1101 error_code BitcodeReader::ResolveGlobalAndAliasInits() {
1100 std::error_code BitcodeReader::ResolveGlobalAndAliasInits() {
11021101 std::vector > GlobalInitWorklist;
11031102 std::vector > AliasInitWorklist;
11041103 std::vector > FunctionPrefixWorklist;
11471146 FunctionPrefixWorklist.pop_back();
11481147 }
11491148
1150 return error_code();
1149 return std::error_code();
11511150 }
11521151
11531152 static APInt ReadWideAPInt(ArrayRef Vals, unsigned TypeBits) {
11581157 return APInt(TypeBits, Words);
11591158 }
11601159
1161 error_code BitcodeReader::ParseConstants() {
1160 std::error_code BitcodeReader::ParseConstants() {
11621161 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
11631162 return Error(InvalidRecord);
11641163
11811180 // Once all the constants have been read, go through and resolve forward
11821181 // references.
11831182 ValueList.ResolveConstantForwardRefs();
1184 return error_code();
1183 return std::error_code();
11851184 case BitstreamEntry::Record:
11861185 // The interesting case.
11871186 break;
15961595 }
15971596 }
15981597
1599 error_code BitcodeReader::ParseUseLists() {
1598 std::error_code BitcodeReader::ParseUseLists() {
16001599 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
16011600 return Error(InvalidRecord);
16021601
16111610 case BitstreamEntry::Error:
16121611 return Error(MalformedBlock);
16131612 case BitstreamEntry::EndBlock:
1614 return error_code();
1613 return std::error_code();
16151614 case BitstreamEntry::Record:
16161615 // The interesting case.
16171616 break;
16361635 /// RememberAndSkipFunctionBody - When we see the block for a function body,
16371636 /// remember where it is and then skip it. This lets us lazily deserialize the
16381637 /// functions.
1639 error_code BitcodeReader::RememberAndSkipFunctionBody() {
1638 std::error_code BitcodeReader::RememberAndSkipFunctionBody() {
16401639 // Get the function we are talking about.
16411640 if (FunctionsWithBodies.empty())
16421641 return Error(InsufficientFunctionProtos);
16511650 // Skip over the function block for now.
16521651 if (Stream.SkipBlock())
16531652 return Error(InvalidRecord);
1654 return error_code();
1655 }
1656
1657 error_code BitcodeReader::GlobalCleanup() {
1653 return std::error_code();
1654 }
1655
1656 std::error_code BitcodeReader::GlobalCleanup() {
16581657 // Patch the initializers for globals and aliases up.
16591658 ResolveGlobalAndAliasInits();
16601659 if (!GlobalInits.empty() || !AliasInits.empty())
16801679 // want lazy deserialization.
16811680 std::vector >().swap(GlobalInits);
16821681 std::vector >().swap(AliasInits);
1683 return error_code();
1684 }
1685
1686 error_code BitcodeReader::ParseModule(bool Resume) {
1682 return std::error_code();
1683 }
1684
1685 std::error_code BitcodeReader::ParseModule(bool Resume) {
16871686 if (Resume)
16881687 Stream.JumpToBit(NextUnreadBit);
16891688 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
17141713 return Error(MalformedBlock);
17151714 break;
17161715 case bitc::PARAMATTR_BLOCK_ID:
1717 if (error_code EC = ParseAttributeBlock())
1716 if (std::error_code EC = ParseAttributeBlock())
17181717 return EC;
17191718 break;
17201719 case bitc::PARAMATTR_GROUP_BLOCK_ID:
1721 if (error_code EC = ParseAttributeGroupBlock())
1720 if (std::error_code EC = ParseAttributeGroupBlock())
17221721 return EC;
17231722 break;
17241723 case bitc::TYPE_BLOCK_ID_NEW:
1725 if (error_code EC = ParseTypeTable())
1724 if (std::error_code EC = ParseTypeTable())
17261725 return EC;
17271726 break;
17281727 case bitc::VALUE_SYMTAB_BLOCK_ID:
1729 if (error_code EC = ParseValueSymbolTable())
1728 if (std::error_code EC = ParseValueSymbolTable())
17301729 return EC;
17311730 SeenValueSymbolTable = true;
17321731 break;
17331732 case bitc::CONSTANTS_BLOCK_ID:
1734 if (error_code EC = ParseConstants())
1733 if (std::error_code EC = ParseConstants())
17351734 return EC;
1736 if (error_code EC = ResolveGlobalAndAliasInits())
1735 if (std::error_code EC = ResolveGlobalAndAliasInits())
17371736 return EC;
17381737 break;
17391738 case bitc::METADATA_BLOCK_ID:
1740 if (error_code EC = ParseMetadata())
1739 if (std::error_code EC = ParseMetadata())
17411740 return EC;
17421741 break;
17431742 case bitc::FUNCTION_BLOCK_ID:
17451744 // FunctionsWithBodies list.
17461745 if (!SeenFirstFunctionBody) {
17471746 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
1748 if (error_code EC = GlobalCleanup())
1747 if (std::error_code EC = GlobalCleanup())
17491748 return EC;
17501749 SeenFirstFunctionBody = true;
17511750 }
17521751
1753 if (error_code EC = RememberAndSkipFunctionBody())
1752 if (std::error_code EC = RememberAndSkipFunctionBody())
17541753 return EC;
17551754 // For streaming bitcode, suspend parsing when we reach the function
17561755 // bodies. Subsequent materialization calls will resume it when
17601759 // just finish the parse now.
17611760 if (LazyStreamer && SeenValueSymbolTable) {
17621761 NextUnreadBit = Stream.GetCurrentBitNo();
1763 return error_code();
1762 return std::error_code();
17641763 }
17651764 break;
17661765 case bitc::USELIST_BLOCK_ID:
1767 if (error_code EC = ParseUseLists())
1766 if (std::error_code EC = ParseUseLists())
17681767 return EC;
17691768 break;
17701769 }
20062005 }
20072006 }
20082007
2009 error_code BitcodeReader::ParseBitcodeInto(Module *M) {
2008 std::error_code BitcodeReader::ParseBitcodeInto(Module *M) {
20102009 TheModule = nullptr;
20112010
2012 if (error_code EC = InitStream())
2011 if (std::error_code EC = InitStream())
20132012 return EC;
20142013
20152014 // Sniff for the signature.
20252024 // need to understand them all.
20262025 while (1) {
20272026 if (Stream.AtEndOfStream())
2028 return error_code();
2027 return std::error_code();
20292028
20302029 BitstreamEntry Entry =
20312030 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
20342033 case BitstreamEntry::Error:
20352034 return Error(MalformedBlock);
20362035 case BitstreamEntry::EndBlock:
2037 return error_code();
2036 return std::error_code();
20382037
20392038 case BitstreamEntry::SubBlock:
20402039 switch (Entry.ID) {
20472046 if (TheModule)
20482047 return Error(InvalidMultipleBlocks);
20492048 TheModule = M;
2050 if (error_code EC = ParseModule(false))
2049 if (std::error_code EC = ParseModule(false))
20512050 return EC;
20522051 if (LazyStreamer)
2053 return error_code();
2052 return std::error_code();
20542053 break;
20552054 default:
20562055 if (Stream.SkipBlock())
20672066 if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 &&
20682067 Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
20692068 Stream.AtEndOfStream())
2070 return error_code();
2069 return std::error_code();
20712070
20722071 return Error(InvalidRecord);
20732072 }
20742073 }
20752074 }
20762075
2077 error_code BitcodeReader::ParseModuleTriple(std::string &Triple) {
2076 std::error_code BitcodeReader::ParseModuleTriple(std::string &Triple) {
20782077 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
20792078 return Error(InvalidRecord);
20802079
20892088 case BitstreamEntry::Error:
20902089 return Error(MalformedBlock);
20912090 case BitstreamEntry::EndBlock:
2092 return error_code();
2091 return std::error_code();
20932092 case BitstreamEntry::Record:
20942093 // The interesting case.
20952094 break;
21102109 }
21112110 }
21122111
2113 error_code BitcodeReader::ParseTriple(std::string &Triple) {
2114 if (error_code EC = InitStream())
2112 std::error_code BitcodeReader::ParseTriple(std::string &Triple) {
2113 if (std::error_code EC = InitStream())
21152114 return EC;
21162115
21172116 // Sniff for the signature.
21322131 case BitstreamEntry::Error:
21332132 return Error(MalformedBlock);
21342133 case BitstreamEntry::EndBlock:
2135 return error_code();
2134 return std::error_code();
21362135
21372136 case BitstreamEntry::SubBlock:
21382137 if (Entry.ID == bitc::MODULE_BLOCK_ID)
21512150 }
21522151
21532152 /// ParseMetadataAttachment - Parse metadata attachments.
2154 error_code BitcodeReader::ParseMetadataAttachment() {
2153 std::error_code BitcodeReader::ParseMetadataAttachment() {
21552154 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
21562155 return Error(InvalidRecord);
21572156
21642163 case BitstreamEntry::Error:
21652164 return Error(MalformedBlock);
21662165 case BitstreamEntry::EndBlock:
2167 return error_code();
2166 return std::error_code();
21682167 case BitstreamEntry::Record:
21692168 // The interesting case.
21702169 break;
21982197 }
21992198
22002199 /// ParseFunctionBody - Lazily parse the specified function body block.
2201 error_code BitcodeReader::ParseFunctionBody(Function *F) {
2200 std::error_code BitcodeReader::ParseFunctionBody(Function *F) {
22022201 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
22032202 return Error(InvalidRecord);
22042203
22342233 return Error(InvalidRecord);
22352234 break;
22362235 case bitc::CONSTANTS_BLOCK_ID:
2237 if (error_code EC = ParseConstants())
2236 if (std::error_code EC = ParseConstants())
22382237 return EC;
22392238 NextValueNo = ValueList.size();
22402239 break;
22412240 case bitc::VALUE_SYMTAB_BLOCK_ID:
2242 if (error_code EC = ParseValueSymbolTable())
2241 if (std::error_code EC = ParseValueSymbolTable())
22432242 return EC;
22442243 break;
22452244 case bitc::METADATA_ATTACHMENT_ID:
2246 if (error_code EC = ParseMetadataAttachment())
2245 if (std::error_code EC = ParseMetadataAttachment())
22472246 return EC;
22482247 break;
22492248 case bitc::METADATA_BLOCK_ID:
2250 if (error_code EC = ParseMetadata())
2249 if (std::error_code EC = ParseMetadata())
22512250 return EC;
22522251 break;
22532252 }
31173116 ValueList.shrinkTo(ModuleValueListSize);
31183117 MDValueList.shrinkTo(ModuleMDValueListSize);
31193118 std::vector().swap(FunctionBBs);
3120 return error_code();
3119 return std::error_code();
31213120 }
31223121
31233122 /// Find the function body in the bitcode stream
3124 error_code BitcodeReader::FindFunctionInStream(Function *F,
3125 DenseMap::iterator DeferredFunctionInfoIterator) {
3123 std::error_code BitcodeReader::FindFunctionInStream(
3124 Function *F,
3125 DenseMap::iterator DeferredFunctionInfoIterator) {
31263126 while (DeferredFunctionInfoIterator->second == 0) {
31273127 if (Stream.AtEndOfStream())
31283128 return Error(CouldNotFindFunctionInStream);
31293129 // ParseModule will parse the next body in the stream and set its
31303130 // position in the DeferredFunctionInfo map.
3131 if (error_code EC = ParseModule(true))
3131 if (std::error_code EC = ParseModule(true))
31323132 return EC;
31333133 }
3134 return error_code();
3134 return std::error_code();
31353135 }
31363136
31373137 //===----------------------------------------------------------------------===//
31473147 return false;
31483148 }
31493149
3150 error_code BitcodeReader::Materialize(GlobalValue *GV) {
3150 std::error_code BitcodeReader::Materialize(GlobalValue *GV) {
31513151 Function *F = dyn_cast(GV);
31523152 // If it's not a function or is already material, ignore the request.
31533153 if (!F || !F->isMaterializable())
3154 return error_code();
3154 return std::error_code();
31553155
31563156 DenseMap::iterator DFII = DeferredFunctionInfo.find(F);
31573157 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
31583158 // If its position is recorded as 0, its body is somewhere in the stream
31593159 // but we haven't seen it yet.
31603160 if (DFII->second == 0 && LazyStreamer)
3161 if (error_code EC = FindFunctionInStream(F, DFII))
3161 if (std::error_code EC = FindFunctionInStream(F, DFII))
31623162 return EC;
31633163
31643164 // Move the bit stream to the saved position of the deferred function body.
31653165 Stream.JumpToBit(DFII->second);
31663166
3167 if (error_code EC = ParseFunctionBody(F))
3167 if (std::error_code EC = ParseFunctionBody(F))
31683168 return EC;
31693169
31703170 // Upgrade any old intrinsic calls in the function.
31793179 }
31803180 }
31813181
3182 return error_code();
3182 return std::error_code();
31833183 }
31843184
31853185 bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
32013201 F->deleteBody();
32023202 }
32033203
3204
3205 error_code BitcodeReader::MaterializeModule(Module *M) {
3204 std::error_code BitcodeReader::MaterializeModule(Module *M) {
32063205 assert(M == TheModule &&
32073206 "Can only Materialize the Module this BitcodeReader is attached to.");
32083207 // Iterate over the module, deserializing any functions that are still on
32103209 for (Module::iterator F = TheModule->begin(), E = TheModule->end();
32113210 F != E; ++F) {
32123211 if (F->isMaterializable()) {
3213 if (error_code EC = Materialize(F))
3212 if (std::error_code EC = Materialize(F))
32143213 return EC;
32153214 }
32163215 }
32433242 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
32443243
32453244 UpgradeDebugInfo(*M);
3246 return error_code();
3247 }
3248
3249 error_code BitcodeReader::InitStream() {
3245 return std::error_code();
3246 }
3247
3248 std::error_code BitcodeReader::InitStream() {
32503249 if (LazyStreamer)
32513250 return InitLazyStream();
32523251 return InitStreamFromBuffer();
32533252 }
32543253
3255 error_code BitcodeReader::InitStreamFromBuffer() {
3254 std::error_code BitcodeReader::InitStreamFromBuffer() {
32563255 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
32573256 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
32583257
32723271 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
32733272 Stream.init(*StreamFile);
32743273
3275 return error_code();
3276 }
3277
3278 error_code BitcodeReader::InitLazyStream() {
3274 return std::error_code();
3275 }
3276
3277 std::error_code BitcodeReader::InitLazyStream() {
32793278 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
32803279 // see it.
32813280 StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer);
32963295 Bytes->dropLeadingBytes(bitcodeStart - buf);
32973296 Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart);
32983297 }
3299 return error_code();
3298 return std::error_code();
33003299 }
33013300
33023301 namespace {
33673366 Module *M = new Module(Buffer->getBufferIdentifier(), Context);
33683367 BitcodeReader *R = new BitcodeReader(Buffer, Context);
33693368 M->setMaterializer(R);
3370 if (error_code EC = R->ParseBitcodeInto(M)) {
3369 if (std::error_code EC = R->ParseBitcodeInto(M)) {
33713370 delete M; // Also deletes R.
33723371 return EC;
33733372 }
33873386 Module *M = new Module(name, Context);
33883387 BitcodeReader *R = new BitcodeReader(streamer, Context);
33893388 M->setMaterializer(R);
3390 if (error_code EC = R->ParseBitcodeInto(M)) {
3389 if (std::error_code EC = R->ParseBitcodeInto(M)) {
33913390 if (ErrMsg)
33923391 *ErrMsg = EC.message();
33933392 delete M; // Also deletes R.
34093408 static_cast(M->getMaterializer())->setBufferOwned(false);
34103409
34113410 // Read in the entire module, and destroy the BitcodeReader.
3412 if (error_code EC = M->materializeAllPermanently()) {
3411 if (std::error_code EC = M->materializeAllPermanently()) {
34133412 delete M;
34143413 return EC;
34153414 }
34283427 R->setBufferOwned(false);
34293428
34303429 std::string Triple("");
3431 if (error_code EC = R->ParseTriple(Triple))
3430 if (std::error_code EC = R->ParseTriple(Triple))
34323431 if (ErrMsg)
34333432 *ErrMsg = EC.message();
34343433
2020 using namespace llvm;
2121 using namespace dwarf;
2222 using namespace object;
23 using std::error_code;
2423
2524 #define DEBUG_TYPE "dwarf"
2625
734733 object::RelocToApply R(V.visit(Type, Reloc, 0, SymAddr));
735734 if (V.error()) {
736735 SmallString<32> Name;
737 error_code ec(Reloc.getTypeName(Name));
736 std::error_code ec(Reloc.getTypeName(Name));
738737 if (ec) {
739738 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
740739 }
1818 #include "llvm/IR/Module.h"
1919 #include
2020 using namespace llvm;
21 using std::error_code;
2221
2322 namespace {
2423
3433 ///
3534 ExecutionEngine *Interpreter::create(Module *M, std::string* ErrStr) {
3635 // Tell this Module to materialize everything and release the GVMaterializer.
37 if (error_code EC = M->materializeAllPermanently()) {
36 if (std::error_code EC = M->materializeAllPermanently()) {
3837 if (ErrStr)
3938 *ErrStr = EC.message();
4039 // We got an error, just return 0
2222
2323 using namespace llvm;
2424 using namespace llvm::object;
25 using std::error_code;
2625
2726 #define DEBUG_TYPE "dyld"
2827
7372 llvm_unreachable("Attempting to remap address of unknown section!");
7473 }
7574
76 static error_code getOffset(const SymbolRef &Sym, uint64_t &Result) {
75 static std::error_code getOffset(const SymbolRef &Sym, uint64_t &Result) {
7776 uint64_t Address;
78 if (error_code EC = Sym.getAddress(Address))
77 if (std::error_code EC = Sym.getAddress(Address))
7978 return EC;
8079
8180 if (Address == UnknownAddressOrSize) {
8584
8685 const ObjectFile *Obj = Sym.getObject();
8786 section_iterator SecI(Obj->section_begin());
88 if (error_code EC = Sym.getSection(SecI))
87 if (std::error_code EC = Sym.getSection(SecI))
8988 return EC;
9089
9190 if (SecI == Obj->section_end()) {
9493 }
9594
9695 uint64_t SectionAddress;
97 if (error_code EC = SecI->getAddress(SectionAddress))
96 if (std::error_code EC = SecI->getAddress(SectionAddress))
9897 return EC;
9998
10099 Result = Address - SectionAddress;
2626
2727 using namespace llvm;
2828 using namespace llvm::object;
29 using std::error_code;
3029
3130 #define DEBUG_TYPE "dyld"
3231
3332 namespace {
3433
35 static inline error_code check(error_code Err) {
34 static inline std::error_code check(std::error_code Err) {
3635 if (Err) {
3736 report_fatal_error(Err.message());
3837 }
5554
5655 public:
5756 DyldELFObject(std::unique_ptr UnderlyingFile,
58 MemoryBuffer *Wrapper, error_code &ec);
59
60 DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
57 MemoryBuffer *Wrapper, std::error_code &ec);
58
59 DyldELFObject(MemoryBuffer *Wrapper, std::error_code &ec);
6160
6261 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
6362 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
109108 // actual memory. Ultimately, the Binary parent class will take ownership of
110109 // this MemoryBuffer object but not the underlying memory.
111110 template
112 DyldELFObject::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec)
111 DyldELFObject::DyldELFObject(MemoryBuffer *Wrapper, std::error_code &ec)
113112 : ELFObjectFile(Wrapper, ec) {
114113 this->isDyldELFObject = true;
115114 }
116115
117116 template
118117 DyldELFObject::DyldELFObject(std::unique_ptr UnderlyingFile,
119 MemoryBuffer *Wrapper, error_code &ec)
118 MemoryBuffer *Wrapper, std::error_code &ec)
120119 : ELFObjectFile(Wrapper, ec),
121120 UnderlyingFile(std::move(UnderlyingFile)) {
122121 this->isDyldELFObject = true;
182181 if (!ObjFile)
183182 return nullptr;
184183
185 error_code ec;
184 std::error_code ec;
186185 MemoryBuffer *Buffer =
187186 MemoryBuffer::getMemBuffer(ObjFile->getData(), "", false);
188187
218217 std::pair Ident =
219218 std::make_pair((uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
220219 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
221 error_code ec;
220 std::error_code ec;
222221
223222 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
224223 auto Obj =
4040 #include
4141
4242 using namespace llvm;
43 using std::error_code;
4443
4544 #define DEBUG_TYPE "ir"
4645
26012600 char **OutMessage) {
26022601
26032602 std::unique_ptr MB;
2604 error_code ec;
2603 std::error_code ec;
26052604 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
26062605 *OutMemBuf = wrap(MB.release());
26072606 return 0;
26142613 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
26152614 char **OutMessage) {
26162615 std::unique_ptr MB;
2617 error_code ec;
2616 std::error_code ec;
26182617 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
26192618 *OutMemBuf = wrap(MB.release());
26202619 return 0;
2121 #include
2222 #include
2323 using namespace llvm;
24 using std::error_code;
2524
2625 //===----------------------------------------------------------------------===//
2726 // GCOVFile implementation.
438437 StringRef Remaining;
439438 public:
440439 LineConsumer(StringRef Filename) {
441 if (error_code EC = MemoryBuffer::getFileOrSTDIN(Filename, Buffer)) {
440 if (std::error_code EC = MemoryBuffer::getFileOrSTDIN(Filename, Buffer)) {
442441 errs() << Filename << ": " << EC.message() << "\n";
443442 Remaining = "";
444443 } else
2727 #include
2828 #include
2929 using namespace llvm;
30 using std::error_code;
3130
3231 //===----------------------------------------------------------------------===//
3332 // Methods to implement the globals and functions lists.
381380 if (!Materializer)
382381 return false;
383382
384 error_code EC = Materializer->Materialize(GV);
383 std::error_code EC = Materializer->Materialize(GV);
385384 if (!EC)
386385 return false;
387386 if (ErrInfo)
394393 return Materializer->Dematerialize(GV);
395394 }
396395
397 error_code Module::materializeAll() {
396 std::error_code Module::materializeAll() {
398397 if (!Materializer)
399 return error_code();
398 return std::error_code();
400399 return Materializer->MaterializeModule(this);
401400 }
402401
403 error_code Module::materializeAllPermanently() {
404 if (error_code EC = materializeAll())
402 std::error_code Module::materializeAllPermanently() {
403 if (std::error_code EC = materializeAll())
405404 return EC;
406405
407406 Materializer.reset();
408 return error_code();
407 return std::error_code();
409408 }
410409
411410 //===----------------------------------------------------------------------===//
2020 #include
2121
2222 using namespace llvm;
23 using std::error_code;
2423
2524 namespace llvm {
2625 extern bool TimePassesIsEnabled;
3635 (const unsigned char *)Buffer->getBufferEnd())) {
3736 std::string ErrMsg;
3837 ErrorOr ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
39 if (error_code EC = ModuleOrErr.getError()) {
38 if (std::error_code EC = ModuleOrErr.getError()) {
4039 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
4140 EC.message());
4241 // ParseBitcodeFile does not take ownership of the Buffer in the
5352 Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
5453 LLVMContext &Context) {
5554 std::unique_ptr File;
56 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
55 if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
5756 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
5857 "Could not open input file: " + ec.message());
5958 return nullptr;
7069 (const unsigned char *)Buffer->getBufferEnd())) {
7170 ErrorOr ModuleOrErr = parseBitcodeFile(Buffer, Context);
7271 Module *M = nullptr;
73 if (error_code EC = ModuleOrErr.getError())
72 if (std::error_code EC = ModuleOrErr.getError())
7473 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
7574 EC.message());
7675 else
8685 Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
8786 LLVMContext &Context) {
8887 std::unique_ptr File;
89 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
88 if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
9089 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
9190 "Could not open input file: " + ec.message());
9291 return nullptr;
5252 #include "llvm/Transforms/ObjCARC.h"
5353 #include
5454 using namespace llvm;
55 using std::error_code;
5655
5756 const char* LTOCodeGenerator::getVersionString() {
5857 #ifdef LLVM_VERSION_INFO
208207 // make unique temp .o file to put generated object file
209208 SmallString<128> Filename;
210209 int FD;
211 error_code EC = sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
210 std::error_code EC =
211 sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
212212 if (EC) {
213213 errMsg = EC.message();
214214 return false;
252252
253253 // read .o file into memory buffer
254254 std::unique_ptr BuffPtr;
255 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
255 if (std::error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
256256 errMsg = ec.message();
257257 sys::fs::remove(NativeObjectPath);
258258 return nullptr;
4242 #include "llvm/Transforms/Utils/GlobalStatus.h"
4343 #include
4444 using namespace llvm;
45 using std::error_code;
4645
4746 LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
4847 : _module(m), _target(t),
9897 LTOModule *LTOModule::makeLTOModule(const char *path, TargetOptions options,
9998 std::string &errMsg) {
10099 std::unique_ptr buffer;
101 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
100 if (std::error_code ec = MemoryBuffer::getFile(path, buffer)) {
102101 errMsg = ec.message();
103102 return nullptr;
104103 }
117116 TargetOptions options,
118117 std::string &errMsg) {
119118 std::unique_ptr buffer;
120 if (error_code ec =
119 if (std::error_code ec =
121120 MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
122121 errMsg = ec.message();
123122 return nullptr;
140139 // parse bitcode buffer
141140 ErrorOr ModuleOrErr =
142141 getLazyBitcodeModule(buffer, getGlobalContext());
143 if (error_code EC = ModuleOrErr.getError()) {
142 if (std::error_code EC = ModuleOrErr.getError()) {
144143 errMsg = EC.message();
145144 delete buffer;
146145 return nullptr;
2727 #include
2828
2929 using namespace llvm;
30 using std::error_code;
3130
3231 MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
3332 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
3938 AllowTemporaryLabels(true), DwarfCompileUnitID(0),
4039 AutoReset(DoAutoReset) {
4140
42 error_code EC = llvm::sys::fs::current_path(CompilationDir);
41 std::error_code EC = llvm::sys::fs::current_path(CompilationDir);
4342 if (EC)
4443 CompilationDir.clear();
4544
2424 #include
2525
2626 namespace llvm {
27 using std::error_code;
2827
2928 namespace {
3029
453452 InstrRegInfoHolder IRI(MII, MRI);
454453 yaml::Input YIn(YamlContent, (void *)&IRI);
455454 YIn >> YAMLModule;
456 if (error_code ec = YIn.error())
455 if (std::error_code ec = YIn.error())
457456 return ec.message();
458457 StringRef err = Parser.parse(YAMLModule);
459458 if (!err.empty())
1919
2020 using namespace llvm;
2121 using namespace object;
22 using std::error_code;
2322
2423 static const char *const Magic = "!\n";
2524
115114 return Child(Parent, NextLoc);
116115 }
117116
118 error_code Archive::Child::getName(StringRef &Result) const {
117 std::error_code Archive::Child::getName(StringRef &Result) const {
119118 StringRef name = getRawName();
120119 // Check if it's a special name.
121120 if (name[0] == '/') {
168167 return object_error::success;
169168 }
170169
171 error_code Archive::Child::getMemoryBuffer(std::unique_ptr &Result,
172 bool FullPath) const {
170 std::error_code
171 Archive::Child::getMemoryBuffer(std::unique_ptr &Result,
172 bool FullPath) const {
173173 StringRef Name;
174 if (error_code ec = getName(Name))
174 if (std::error_code ec = getName(Name))
175175 return ec;
176176 SmallString<128> Path;
177177 Result.reset(MemoryBuffer::getMemBuffer(
179179 .toStringRef(Path)
180180 : Name,
181181 false));
182 return error_code();
183 }
184
185 error_code Archive::Child::getAsBinary(std::unique_ptr &Result,
186 LLVMContext *Context) const {
182 return std::error_code();
183 }
184
185 std::error_code Archive::Child::getAsBinary(std::unique_ptr &Result,
186 LLVMContext *Context) const {
187187 std::unique_ptr ret;
188188 std::unique_ptr Buff;
189 if (error_code ec = getMemoryBuffer(Buff))
189 if (std::error_code ec = getMemoryBuffer(Buff))
190190 return ec;
191191 ErrorOr BinaryOrErr = createBinary(Buff.release(), Context);
192 if (error_code EC = BinaryOrErr.getError())
192 if (std::error_code EC = BinaryOrErr.getError())
193193 return EC;
194194 Result.reset(BinaryOrErr.get());
195195 return object_error::success;
196196 }
197197
198198 ErrorOr Archive::create(MemoryBuffer *Source) {
199 error_code EC;
199 std::error_code EC;
200200 std::unique_ptr Ret(new Archive(Source, EC));
201201 if (EC)
202202 return EC;
203203 return Ret.release();
204204 }
205205
206 Archive::Archive(MemoryBuffer *source, error_code &ec)
207 : Binary(Binary::ID_Archive, source), SymbolTable(child_end()) {
206 Archive::Archive(MemoryBuffer *source, std::error_code &ec)
207 : Binary(Binary::ID_Archive, source), SymbolTable(child_end()) {
208208 // Check for sufficient magic.
209209 assert(source);
210210 if (source->getBufferSize() < 8 ||
335335 return Child(this, nullptr);
336336 }
337337
338 error_code Archive::Symbol::getName(StringRef &Result) const {
338 std::error_code Archive::Symbol::getName(StringRef &Result) const {
339339 Result = StringRef(Parent->SymbolTable->getBuffer().begin() + StringIndex);
340340 return object_error::success;
341341 }
342342
343 error_code Archive::Symbol::getMember(child_iterator &Result) const {
343 std::error_code Archive::Symbol::getMember(child_iterator &Result) const {
344344 const char *Buf = Parent->SymbolTable->getBuffer().begin();
345345 const char *Offsets = Buf + 4;
346346 uint32_t Offset = 0;
2323
2424 using namespace llvm;
2525 using namespace object;
26 using std::error_code;
2726
2827 using support::ulittle8_t;
2928 using support::ulittle16_t;
3130 using support::little16_t;
3231
3332 // Returns false if size is greater than the buffer size. And sets ec.
34 static bool checkSize(const MemoryBuffer *M, error_code &EC, uint64_t Size) {
33 static bool checkSize(const MemoryBuffer *M, std::error_code &EC,
34 uint64_t Size) {
3535 if (M->getBufferSize() < Size) {
3636 EC = object_error::unexpected_eof;
3737 return false;
4141
4242 // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
4343 // Returns unexpected_eof if error.
44 template
45 static error_code getObject(const T *&Obj, const MemoryBuffer *M,
46 const uint8_t *Ptr, const size_t Size = sizeof(T)) {
44 template
45 static std::error_code getObject(const T *&Obj, const MemoryBuffer *M,
46 const uint8_t *Ptr,
47 const size_t Size = sizeof(T)) {
4748 uintptr_t Addr = uintptr_t(Ptr);
4849 if (Addr + Size < Addr ||
4950 Addr + Size < Size ||
129130 Ref.p = reinterpret_cast(Symb);
130131 }
131132
132 error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
133 StringRef &Result) const {
133 std::error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
134 StringRef &Result) const {
134135 const coff_symbol *Symb = toSymb(Ref);
135136 return getSymbolName(Symb, Result);
136137 }
137138
138 error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
139 uint64_t &Result) const {
139 std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
140 uint64_t &Result) const {
140141 const coff_symbol *Symb = toSymb(Ref);
141142 const coff_section *Section = nullptr;
142 if (error_code EC = getSection(Symb->SectionNumber, Section))
143 if (std::error_code EC = getSection(Symb->SectionNumber, Section))
143144 return EC;
144145
145146 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
151152 return object_error::success;
152153 }
153154
154 error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
155 SymbolRef::Type &Result) const {
155 std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
156 SymbolRef::Type &Result) const {
156157 const coff_symbol *Symb = toSymb(Ref);
157158 Result = SymbolRef::ST_Other;
158159 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
164165 uint32_t Characteristics = 0;
165166 if (!COFF::isReservedSectionNumber(Symb->SectionNumber)) {
166167 const coff_section *Section = nullptr;
167 if (error_code EC = getSection(Symb->SectionNumber, Section))
168 if (std::error_code EC = getSection(Symb->SectionNumber, Section))
168169 return EC;
169170 Characteristics = Section->Characteristics;
170171 }
202203 return Result;
203204 }
204205
205 error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
206 uint64_t &Result) const {
206 std::error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
207 uint64_t &Result) const {
207208 // FIXME: Return the correct size. This requires looking at all the symbols
208209 // in the same section as this symbol, and looking for either the next
209210 // symbol, or the end of the section.
210211 const coff_symbol *Symb = toSymb(Ref);
211212 const coff_section *Section = nullptr;
212 if (error_code EC = getSection(Symb->SectionNumber, Section))
213 if (std::error_code EC = getSection(Symb->SectionNumber, Section))
213214 return EC;
214215
215216 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
221222 return object_error::success;
222223 }
223224
224 error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref,
225 section_iterator &Result) const {
225 std::error_code
226 COFFObjectFile::getSymbolSection(DataRefImpl Ref,
227 section_iterator &Result) const {
226228 const coff_symbol *Symb = toSymb(Ref);
227229 if (COFF::isReservedSectionNumber(Symb->SectionNumber)) {
228230 Result = section_end();
229231 } else {
230232 const coff_section *Sec = nullptr;
231 if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC;
233 if (std::error_code EC = getSection(Symb->SectionNumber, Sec))
234 return EC;
232235 DataRefImpl Ref;
233236 Ref.p = reinterpret_cast(Sec);
234237 Result = section_iterator(SectionRef(Ref, this));
242245 Ref.p = reinterpret_cast(Sec);
243246 }
244247
245 error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
246 StringRef &Result) const {
248 std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
249 StringRef &Result) const {
247250 const coff_section *Sec = toSec(Ref);
248251 return getSectionName(Sec, Result);
249252 }
250253
251 error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
252 uint64_t &Result) const {
254 std::error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
255 uint64_t &Result) const {
253256 const coff_section *Sec = toSec(Ref);
254257 Result = Sec->VirtualAddress;
255258 return object_error::success;
256259 }
257260
258 error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
259 uint64_t &Result) const {
261 std::error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
262 uint64_t &Result) const {
260263 const coff_section *Sec = toSec(Ref);
261264 Result = Sec->SizeOfRawData;
262265 return object_error::success;
263266 }
264267
265 error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
266 StringRef &Result) const {
268 std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
269 StringRef &Result) const {
267270 const coff_section *Sec = toSec(Ref);
268271 ArrayRef Res;
269 error_code EC = getSectionContents(Sec, Res);
272 std::error_code EC = getSectionContents(Sec, Res);
270273 Result = StringRef(reinterpret_cast(Res.data()), Res.size());
271274 return EC;
272275 }
273276
274 error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
275 uint64_t &Res) const {
277 std::error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
278 uint64_t &Res) const {
276279 const coff_section *Sec = toSec(Ref);
277280 if (!Sec)
278281 return object_error::parse_failed;
280283 return object_error::success;
281284 }
282285
283 error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
284 bool &Result) const {
286 std::error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
287 bool &Result) const {
285288 const coff_section *Sec = toSec(Ref);
286289 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
287290 return object_error::success;
288291 }
289292
290 error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
291 bool &Result) const {
293 std::error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
294 bool &Result) const {
292295 const coff_section *Sec = toSec(Ref);
293296 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
294297 return object_error::success;
295298 }
296299
297 error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
298 bool &Result) const {
300 std::error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
301 bool &Result) const {
299302 const coff_section *Sec = toSec(Ref);
300303 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
301304 return object_error::success;
302305 }
303306
304 error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
305 bool &Result) const {
307 std::error_code
308 COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
309 bool &Result) const {
306310 // FIXME: Unimplemented
307311 Result = true;
308312 return object_error::success;
309313 }
310314
311 error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
312 bool &Result) const {
315 std::error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
316 bool &Result) const {
313317 const coff_section *Sec = toSec(Ref);
314318 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
315319 return object_error::success;
316320 }
317321
318 error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
319 bool &Result) const {
322 std::error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
323 bool &Result) const {
320324 // FIXME: Unimplemented.
321325 Result = false;
322326 return object_error::success;
323327 }
324328
325 error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
326 bool &Result) const {
329 std::error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
330 bool &Result) const {
327331 // FIXME: Unimplemented.
328332 Result = false;
329333 return object_error::success;
330334 }
331335
332 error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
333 DataRefImpl SymbRef,
334 bool &Result) const {
336 std::error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
337 DataRefImpl SymbRef,
338 bool &Result) const {
335339 const coff_section *Sec = toSec(SecRef);
336340 const coff_symbol *Symb = toSymb(SymbRef);
337341 const coff_section *SymbSec = nullptr;
338 if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC;
342 if (std::error_code EC = getSection(Symb->SectionNumber, SymbSec))
343 return EC;
339344 if (SymbSec == Sec)
340345 Result = true;
341346 else
390395 }
391396
392397 // Initialize the pointer to the symbol table.
393 error_code COFFObjectFile::initSymbolTablePtr() {
394 if (error_code EC = getObject(
398 std::error_code COFFObjectFile::initSymbolTablePtr() {
399 if (std::error_code EC = getObject(
395400 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
396401 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
397402 return EC;
403408 base() + COFFHeader->PointerToSymbolTable +
404409 COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
405410 const ulittle32_t *StringTableSizePtr;
406 if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
411 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
407412 return EC;
408413 StringTableSize = *StringTableSizePtr;
409 if (error_code EC =
410 getObject(StringTable, Data, StringTableAddr, StringTableSize))
414 if (std::error_code EC =
415 getObject(StringTable, Data, StringTableAddr, StringTableSize))
411416 return EC;
412417
413418 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
422427 }
423428
424429 // Returns the file offset for the given VA.
425 error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
430 std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
426431 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
427432 : (uint64_t)PE32PlusHeader->ImageBase;
428433 uint64_t Rva = Addr - ImageBase;
431436 }
432437
433438 // Returns the file offset for the given RVA.
434 error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
439 std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
435440 for (const SectionRef &S : sections()) {
436441 const coff_section *Section = getCOFFSection(S);
437442 uint32_t SectionStart = Section->VirtualAddress;
447452
448453 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
449454 // table entry.
450 error_code COFFObjectFile::
451 getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
455 std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
456 StringRef &Name) const {
452457 uintptr_t IntPtr = 0;
453 if (error_code EC = getRvaPtr(Rva, IntPtr))
458 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
454459 return EC;
455460 const uint8_t *Ptr = reinterpret_cast(IntPtr);
456461 Hint = *reinterpret_cast(Ptr);
459464 }
460465
461466 // Find the import table.
462 error_code COFFObjectFile::initImportTablePtr() {
467 std::error_code COFFObjectFile::initImportTablePtr() {
463468 // First, we get the RVA of the import table. If the file lacks a pointer to
464469 // the import table, do nothing.
465470 const data_directory *DataEntry;
477482 // Find the section that contains the RVA. This is needed because the RVA is
478483 // the import table's memory address which is different from its file offset.
479484 uintptr_t IntPtr = 0;
480 if (error_code EC = getRvaPtr(ImportTableRva, IntPtr))
485 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
481486 return EC;
482487 ImportDirectory = reinterpret_cast<
483488 const import_directory_table_entry *>(IntPtr);
485490 }
486491
487492 // Find the export table.
488 error_code COFFObjectFile::initExportTablePtr() {
493 std::error_code COFFObjectFile::initExportTablePtr() {
489494 // First, we get the RVA of the export table. If the file lacks a pointer to
490495 // the export table, do nothing.
491496 const data_directory *DataEntry;
498503
499504 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
500505 uintptr_t IntPtr = 0;
501 if (error_code EC = getRvaPtr(ExportTableRva, IntPtr))
506 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
502507 return EC;
503508 ExportDirectory =
504509 reinterpret_cast(IntPtr);
505510 return object_error::success;
506511 }
507512
508 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC,
513 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, std::error_code &EC,
509514 bool BufferOwned)
510515 : ObjectFile(Binary::ID_COFF, Object, BufferOwned), COFFHeader(nullptr),
511516 PE32Header(nullptr), PE32PlusHeader(nullptr), DataDirectory(nullptr),
686691
687692 // This method is kept here because lld uses this. As soon as we make
688693 // lld to use getCOFFHeader, this method will be removed.
689 error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
694 std::error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
690695 return getCOFFHeader(Res);
691696 }
692697
693 error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
698 std::error_code
699 COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
694700 Res = COFFHeader;
695701 return object_error::success;
696702 }
697703
698 error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
704 std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
699705 Res = PE32Header;
700706 return object_error::success;
701707 }
702708
703 error_code
709 std::error_code
704710 COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
705711 Res = PE32PlusHeader;
706712 return object_error::success;
707713 }
708714
709 error_code COFFObjectFile::getDataDirectory(uint32_t Index,
710 const data_directory *&Res) const {
715 std::error_code
716 COFFObjectFile::getDataDirectory(uint32_t Index,
717 const data_directory *&Res) const {
711718 // Error if if there's no data directory or the index is out of range.
712719 if (!DataDirectory)
713720 return object_error::parse_failed;
720727 return object_error::success;
721728 }
722729
723 error_code COFFObjectFile::getSection(int32_t Index,
724 const coff_section *&Result) const {
730 std::error_code COFFObjectFile::getSection(int32_t Index,
731 const coff_section *&Result) const {
725732 // Check for special index values.
726733 if (COFF::isReservedSectionNumber(Index))
727734 Result = nullptr;
733740 return object_error::success;
734741 }
735742
736 error_code COFFObjectFile::getString(uint32_t Offset,
737 StringRef &Result) const {
743 std::error_code COFFObjectFile::getString(uint32_t Offset,
744 StringRef &Result) const {
738745 if (StringTableSize <= 4)
739746 // Tried to get a string from an empty string table.
740747 return object_error::parse_failed;
744751 return object_error::success;
745752 }
746753
747 error_code COFFObjectFile::getSymbol(uint32_t Index,
748 const coff_symbol *&Result) const {
754 std::error_code COFFObjectFile::getSymbol(uint32_t Index,
755 const coff_symbol *&Result) const {
749756 if (Index < COFFHeader->NumberOfSymbols)
750757 Result = SymbolTable + Index;
751758 else
753760 return object_error::success;
754761 }
755762
756 error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol,
757 StringRef &Res) const {
763 std::error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol,
764 StringRef &Res) const {
758765 // Check for string table entry. First 4 bytes are 0.
759766 if (Symbol->Name.Offset.Zeroes == 0) {
760767 uint32_t Offset = Symbol->Name.Offset.Offset;
761 if (error_code EC = getString(Offset, Res))
768 if (std::error_code EC = getString(Offset, Res))
762769 return EC;
763770 return object_error::success;
764771 }
795802 Symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
796803 }
797804
798 error_code COFFObjectFile::getSectionName(const coff_section *Sec,
799 StringRef &Res) const {
805 std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
806 StringRef &Res) const {
800807 StringRef Name;
801808 if (Sec->Name[7] == 0)
802809 // Null terminated, let ::strlen figure out the length.
815822 if (Name.substr(1).getAsInteger(10, Offset))
816823 return object_error::parse_failed;
817824 }
818 if (error_code EC = getString(Offset, Name))
825 if (std::error_code EC = getString(Offset, Name))
819826 return EC;
820827 }
821828
823830 return object_error::success;
824831 }
825832
826 error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
827 ArrayRef &Res) const {
833 std::error_code
834 COFFObjectFile::getSectionContents(const coff_section *Sec,
835 ArrayRef &Res) const {
828836 // The only thing that we need to verify is that the contents is contained
829837 // within the file bounds. We don't need to make sure it doesn't cover other
830838 // data, as there's nothing that says that is not allowed.
846854 reinterpret_cast(Rel.p) + 1);
847855 }
848856
849 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
850 uint64_t &Res) const {
857 std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
858 uint64_t &Res) const {
851859 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
852860 }
853861
854 error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
855 uint64_t &Res) const {
862 std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
863 uint64_t &Res) const {
856864 Res = toRel(Rel)->VirtualAddress;
857865 return object_error::success;
858866 }
864872 return symbol_iterator(SymbolRef(Ref, this));
865873 }
866874
867 error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
868 uint64_t &Res) const {
875 std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
876 uint64_t &Res) const {
869877 const coff_relocation* R = toRel(Rel);
870878 Res = R->Type;
871879 return object_error::success;
891899 Res = #reloc_type; \
892900 break;
893901
894 error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
895 SmallVectorImpl &Result) const {
902 std::error_code
903 COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
904 SmallVectorImpl &Result) const {
896905 const coff_relocation *Reloc = toRel(Rel);
897906 StringRef Res;
898907 switch (COFFHeader->Machine) {
966975
967976 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
968977
969 error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
970 SmallVectorImpl &Result) const {
978 std::error_code
979 COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
980 SmallVectorImpl &Result) const {
971981 const coff_relocation *Reloc = toRel(Rel);
972982 const coff_symbol *Symb = nullptr;
973 if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC;
983 if (std::error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb))
984 return EC;
974985 DataRefImpl Sym;
975986 Sym.p = reinterpret_cast(Symb);
976987 StringRef SymName;
977 if (error_code EC = getSymbolName(Sym, SymName)) return EC;
988 if (std::error_code EC = getSymbolName(Sym, SymName))
989 return EC;
978990 Result.append(SymName.begin(), SymName.end());
979991 return object_error::success;
980992 }
981993
982 error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
983 LibraryRef &Result) const {
994 std::error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
995 LibraryRef &Result) const {
984996 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
985997 }
986998
987 error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
988 StringRef &Result) const {
999 std::error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
1000 StringRef &Result) const {
9891001 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
9901002 }
9911003
9981010 ++Index;
9991011 }
10001012
1001 error_code ImportDirectoryEntryRef::
1002 getImportTableEntry(const import_directory_table_entry *&Result) const {
1013 std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1014 const import_directory_table_entry *&Result) const {
10031015 Result = ImportTable;
10041016 return object_error::success;
10051017 }
10061018
1007 error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
1019 std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
10081020 uintptr_t IntPtr = 0;
1009 if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
1021 if (std::error_code EC =
1022 OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
10101023 return EC;
10111024 Result = StringRef(reinterpret_cast(IntPtr));
10121025 return object_error::success;
10131026 }
10141027
1015 error_code ImportDirectoryEntryRef::getImportLookupEntry(
1028 std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
10161029 const import_lookup_table_entry32 *&Result) const {
10171030 uintptr_t IntPtr = 0;
1018 if (error_code EC =
1031 if (std::error_code EC =
10191032 OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
10201033 return EC;
10211034 Result = reinterpret_cast(IntPtr);
10331046
10341047 // Returns the name of the current export symbol. If the symbol is exported only
10351048 // by ordinal, the empty string is set as a result.
1036 error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
1049 std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
10371050 uintptr_t IntPtr = 0;
1038 if (error_code EC = OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
1051 if (std::error_code EC =
1052 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
10391053 return EC;
10401054 Result = StringRef(reinterpret_cast(IntPtr));
10411055 return object_error::success;
10421056 }
10431057
10441058 // Returns the starting ordinal number.
1045 error_code ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
1059 std::error_code
1060 ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
10461061 Result = ExportTable->OrdinalBase;
10471062 return object_error::success;
10481063 }
10491064
10501065 // Returns the export ordinal of the current export symbol.
1051 error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
1066 std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
10521067 Result = ExportTable->OrdinalBase + Index;
10531068 return object_error::success;
10541069 }
10551070
10561071 // Returns the address of the current export symbol.
1057 error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
1072 std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
10581073 uintptr_t IntPtr = 0;
1059 if (error_code EC = OwningObject->getRvaPtr(
1060 ExportTable->ExportAddressTableRVA, IntPtr))
1074 if (std::error_code EC =
1075 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
10611076 return EC;
10621077 const export_address_table_entry *entry =
10631078 reinterpret_cast(IntPtr);
10671082
10681083 // Returns the name of the current export symbol. If the symbol is exported only
10691084 // by ordinal, the empty string is set as a result.
1070 error_code ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
1085 std::error_code
1086 ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
10711087 uintptr_t IntPtr = 0;
1072 if (error_code EC = OwningObject->getRvaPtr(
1073 ExportTable->OrdinalTableRVA, IntPtr))
1088 if (std::error_code EC =
1089 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
10741090 return EC;
10751091 const ulittle16_t *Start = reinterpret_cast(IntPtr);
10761092
10801096 I < E; ++I, ++Offset) {
10811097 if (*I != Index)
10821098 continue;
1083 if (error_code EC = OwningObject->getRvaPtr(
1084 ExportTable->NamePointerRVA, IntPtr))
1099 if (std::error_code EC =
1100 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
10851101 return EC;
10861102 const ulittle32_t *NamePtr = reinterpret_cast(IntPtr);
1087 if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1103 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
10881104 return EC;
10891105 Result = StringRef(reinterpret_cast(IntPtr));
10901106 return object_error::success;
10951111
10961112 ErrorOr ObjectFile::createCOFFObjectFile(MemoryBuffer *Object,
10971113 bool BufferOwned) {
1098 error_code EC;
1114 std::error_code EC;
10991115 std::unique_ptr Ret(
11001116 new COFFObjectFile(Object, EC, BufferOwned));
11011117 if (EC)
1414 #include "llvm/Support/MathExtras.h"
1515
1616 namespace llvm {
17 using std::error_code;
1817 using namespace object;
1918
2019 ErrorOr ObjectFile::createELFObjectFile(MemoryBuffer *Obj,
2322 std::size_t MaxAlignment =
2423 1ULL << countTrailingZeros(uintptr_t(Obj->getBufferStart()));
2524
26 error_code EC;
25 std::error_code EC;
2726 std::unique_ptr R;
2827 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
2928 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
1818 #include "llvm/Support/raw_ostream.h"
1919 using namespace llvm;
2020 using namespace object;
21 using std::error_code;
2221
23 IRObjectFile::IRObjectFile(MemoryBuffer *Object, error_code &EC,
22 IRObjectFile::IRObjectFile(MemoryBuffer *Object, std::error_code &EC,
2423 LLVMContext &Context, bool BufferOwned)
2524 : SymbolicFile(Binary::ID_IR, Object, BufferOwned) {
2625 ErrorOr MOrErr = parseBitcodeFile(Object, Context);
9291 Symb.p = Res;
9392 }
9493
95 error_code IRObjectFile::printSymbolName(raw_ostream &OS,
96 DataRefImpl Symb) const {
94 std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
95 DataRefImpl Symb) const {
9796 const GlobalValue &GV = getGV(Symb);
9897
9998 if (Mang)
142141
143142 ErrorOr llvm::object::SymbolicFile::createIRObjectFile(
144143 MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) {
145 error_code EC;
144 std::error_code EC;
146145 std::unique_ptr Ret(
147146 new IRObjectFile(Object, EC, Context, BufferOwned));
148147 if (EC)
2727 using namespace object;
2828
2929 namespace llvm {
30 using std::error_code;
3130
3231 namespace object {
3332
2020
2121 using namespace llvm;
2222 using namespace object;
23 using std::error_code;
2423
2524 template
2625 static void SwapValue(T &Value) {
7271 }
7372 }
7473
75 error_code MachOUniversalBinary::ObjectForArch::getAsObjectFile(
74 std::error_code MachOUniversalBinary::ObjectForArch::getAsObjectFile(
7675 std::unique_ptr &Result) const {
7776 if (Parent) {
7877 StringRef ParentData = Parent->getData();
8382 MemoryBuffer *ObjBuffer = MemoryBuffer::getMemBuffer(
8483 ObjectData, ObjectName, false);
8584 ErrorOr Obj = ObjectFile::createMachOObjectFile(ObjBuffer);
86 if (error_code EC = Obj.getError())
85 if (std::error_code EC = Obj.getError())
8786 return EC;
8887 Result.reset(Obj.get());
8988 return object_error::success;
9190 return object_error::parse_failed;
9291 }
9392
94 error_code MachOUniversalBinary::ObjectForArch::getAsArchive(
93 std::error_code MachOUniversalBinary::ObjectForArch::getAsArchive(
9594 std::unique_ptr &Result) const {
9695 if (Parent) {
9796 StringRef ParentData = Parent->getData();
102101 MemoryBuffer *ObjBuffer = MemoryBuffer::getMemBuffer(
103102 ObjectData, ObjectName, false);
104103 ErrorOr Obj = Archive::create(ObjBuffer);
105 if (error_code EC = Obj.getError())
104 if (std::error_code EC = Obj.getError())
106105 return EC;
107106 Result.reset(Obj.get());
108107 return object_error::success;
114113
115114 ErrorOr
116115 MachOUniversalBinary::create(MemoryBuffer *Source) {
117 error_code EC;
116 std::error_code EC;
118117 std::unique_ptr Ret(
119118 new MachOUniversalBinary(Source, EC));
120119 if (EC)
123122 }
124123
125124 MachOUniversalBinary::MachOUniversalBinary(MemoryBuffer *Source,
126 error_code &ec)
127 : Binary(Binary::ID_MachOUniversalBinary, Source),
128 NumberOfObjects(0) {
125 std::error_code &ec)
126 : Binary(Binary::ID_MachOUniversalBinary, Source), NumberOfObjects(0) {
129127 if (Source->getBufferSize() < sizeof(MachO::fat_header)) {
130128 ec = object_error::invalid_file_type;
131129 return;
155153 }
156154 }
157155
158 error_code MachOUniversalBinary::getObjectForArch(
156 std::error_code MachOUniversalBinary::getObjectForArch(
159157 Triple::ArchType Arch, std::unique_ptr &Result) const {
160158 MachO::CPUType CTM;
161159 if (!getCTMForArch(Arch, CTM))
1717
1818 using namespace llvm;
1919 using namespace object;
20 using std::error_code;
2120
2221 inline ObjectFile *unwrap(LLVMObjectFileRef OF) {
2322 return reinterpret_cast(OF);
8988
9089 void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
9190 LLVMSymbolIteratorRef Sym) {
92 if (error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
91 if (std::error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
9392 report_fatal_error(ec.message());
9493 }
9594
115114 // SectionRef accessors
116115 const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
117116 StringRef ret;
118 if (error_code ec = (*unwrap(SI))->getName(ret))
117 if (std::error_code ec = (*unwrap(SI))->getName(ret))
119118 report_fatal_error(ec.message());
120119 return ret.data();
121120 }
122121
123122 uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
124123 uint64_t ret;
125 if (error_code ec = (*unwrap(SI))->getSize(ret))
124 if (std::error_code ec = (*unwrap(SI))->getSize(ret))
126125 report_fatal_error(ec.message());
127126 return ret;
128127 }
129128
130129 const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
131130 StringRef ret;
132 if (error_code ec = (*unwrap(SI))->getContents(ret))
131 if (std::error_code ec = (*unwrap(SI))->getContents(ret))
133132 report_fatal_error(ec.message());
134133 return ret.data();
135134 }
136135
137136 uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
138137 uint64_t ret;
139 if (error_code ec = (*unwrap(SI))->getAddress(ret))
138 if (std::error_code ec = (*unwrap(SI))->getAddress(ret))
140139 report_fatal_error(ec.message());
141140 return ret;
142141 }
144143 LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
145144 LLVMSymbolIteratorRef Sym) {
146145 bool ret;
147 if (error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret))
146 if (std::error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret))
148147 report_fatal_error(ec.message());
149148 return ret;
150149 }
172171 // SymbolRef accessors
173172 const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
174173 StringRef ret;
175 if (error_code ec = (*unwrap(SI))->getName(ret))
174 if (std::error_code ec = (*unwrap(SI))->getName(ret))
176175 report_fatal_error(ec.message());
177176 return ret.data();
178177 }
179178
180179 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
181180 uint64_t ret;
182 if (error_code ec = (*unwrap(SI))->getAddress(ret))
181 if (std::error_code ec = (*unwrap(SI))->getAddress(ret))
183182 report_fatal_error(ec.message());
184183 return ret;
185184 }
186185
187186 uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
188187 uint64_t ret;
189 if (error_code ec = (*unwrap(SI))->getSize(ret))
188 if (std::error_code ec = (*unwrap(SI))->getSize(ret))
190189 report_fatal_error(ec.message());
191190 return ret;
192191 }
194193 // RelocationRef accessors
195194 uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) {
196195 uint64_t ret;
197 if (error_code ec = (*unwrap(RI))->getAddress(ret))
196 if (std::error_code ec = (*unwrap(RI))->getAddress(ret))
198197 report_fatal_error(ec.message());
199198 return ret;
200199 }
201200
202201 uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
203202 uint64_t ret;
204 if (error_code ec = (*unwrap(RI))->getOffset(ret))
203 if (std::error_code ec = (*unwrap(RI))->getOffset(ret))
205204 report_fatal_error(ec.message());
206205 return ret;
207206 }
213212
214213 uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
215214 uint64_t ret;
216 if (error_code ec = (*unwrap(RI))->getType(ret))
215 if (std::error_code ec = (*unwrap(RI))->getType(ret))
217216 report_fatal_error(ec.message());
218217 return ret;
219218 }
221220 // NOTE: Caller takes ownership of returned string.
222221 const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
223222 SmallVector ret;
224 if (error_code ec = (*unwrap(RI))->getTypeName(ret))
223 if (std::error_code ec = (*unwrap(RI))->getTypeName(ret))
225224 report_fatal_error(ec.message());
226225
227226 char *str = static_cast(malloc(ret.size()));
232231 // NOTE: Caller takes ownership of returned string.
233232 const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
234233 SmallVector ret;
235 if (error_code ec = (*unwrap(RI))->getValueString(ret))
234 if (std::error_code ec = (*unwrap(RI))->getValueString(ret))
236235 report_fatal_error(ec.message());
237236
238237 char *str = static_cast(malloc(ret.size()));
1919
2020 using namespace llvm;
2121 using namespace object;
22 using std::error_code;
2322
2423 void ObjectFile::anchor() { }
2524
2726 bool BufferOwned)
2827 : SymbolicFile(Type, Source, BufferOwned) {}
2928
30 error_code ObjectFile::printSymbolName(raw_ostream &OS,
31 DataRefImpl Symb) const {
29 std::error_code ObjectFile::printSymbolName(raw_ostream &OS,
30 DataRefImpl Symb) const {
3231 StringRef Name;
33 if (error_code EC = getSymbolName(Symb, Name))
32 if (std::error_code EC = getSymbolName(Symb, Name))
3433 return EC;
3534 OS << Name;
3635 return object_error::success;
3736 }
3837
39 error_code ObjectFile::getSymbolAlignment(DataRefImpl DRI,
40 uint32_t &Result) const {
38 std::error_code ObjectFile::getSymbolAlignment(DataRefImpl DRI,
39 uint32_t &Result) const {
4140 Result = 0;
4241 return object_error::success;
4342 }
8786
8887 ErrorOr ObjectFile::createObjectFile(StringRef ObjectPath) {
8988 std::unique_ptr File;
90 if (error_code EC = MemoryBuffer::getFile(ObjectPath, File))
89 if (std::error_code EC = MemoryBuffer::getFile(ObjectPath, File))
9190 return EC;
9291 return createObjectFile(File.release());
9392 }
1919 #include
2020
2121 using namespace llvm;
22 using std::error_code;
23
24 static error_code setupMemoryBuffer(std::string Path,
25 std::unique_ptr &Buffer) {
26 if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer))
22
23 static std::error_code
24 setupMemoryBuffer(std::string Path, std::unique_ptr &Buffer) {
25 if (std::error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer))
2726 return EC;
2827
2928 // Sanity check the file.
3231 return instrprof_error::success;
3332 }
3433
35 static error_code initializeReader(InstrProfReader &Reader) {
34 static std::error_code initializeReader(InstrProfReader &Reader) {
3635 return Reader.readHeader();
3736 }
3837
39 error_code InstrProfReader::create(std::string Path,
40 std::unique_ptr &Result) {
38 std::error_code
39 InstrProfReader::create(std::string Path,
40 std::unique_ptr &Result) {
4141 // Set up the buffer to read.
4242 std::unique_ptr Buffer;
43 if (error_code EC = setupMemoryBuffer(Path, Buffer))
43 if (std::error_code EC = setupMemoryBuffer(Path, Buffer))
4444 return EC;
4545
4646 // Create the reader.
5757 return initializeReader(*Result);
5858 }
5959
60 error_code IndexedInstrProfReader::create(
60 std::error_code IndexedInstrProfReader::create(
6161 std::string Path, std::unique_ptr &Result) {
6262 // Set up the buffer to read.
6363 std::unique_ptr Buffer;
64 if (error_code EC = setupMemoryBuffer(Path, Buffer))
64 if (std::error_code EC = setupMemoryBuffer(Path, Buffer))
6565 return EC;
6666
6767 // Create the reader.
7878 *this = InstrProfIterator();
7979 }
8080
81 error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
81 std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
8282 // Skip empty lines.
8383 while (!Line.is_at_end() && Line->empty())
8484 ++Line;
161161 }
162162
163163 template
164 error_code RawInstrProfReader::readHeader() {
164 std::error_code RawInstrProfReader::readHeader() {
165165 if (!hasFormat(*DataBuffer))
166166 return error(instrprof_error::bad_magic);
167167 if (DataBuffer->getBufferSize() < sizeof(RawHeader))
173173 }
174174
175175 template
176 error_code RawInstrProfReader::readNextHeader(const char *CurrentPos) {
176 std::error_code
177 RawInstrProfReader::readNextHeader(const char *CurrentPos) {
177178 const char *End = DataBuffer->getBufferEnd();
178179 // Skip zero padding between profiles.
179180 while (CurrentPos != End && *CurrentPos == 0)
200201 }
201202
202203 template
203 error_code RawInstrProfReader::readHeader(const RawHeader &Header) {
204 std::error_code
205 RawInstrProfReader::readHeader(const RawHeader &Header) {
204206 if (swap(Header.Version) != getRawVersion())
205207 return error(instrprof_error::unsupported_version);
206208
229231 }
230232
231233 template
232 error_code
234 std::error_code
233235 RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
234236 if (Data == DataEnd)
235 if (error_code EC = readNextHeader(ProfileEnd))
237 if (std::error_code EC = readNextHeader(ProfileEnd))
236238 return EC;
237239
238240 // Get the raw data.
286288 return Magic == IndexedInstrProf::Magic;
287289 }
288290
289 error_code IndexedInstrProfReader::readHeader() {
291 std::error_code IndexedInstrProfReader::readHeader() {
290292 const unsigned char *Start =
291293 (const unsigned char *)DataBuffer->getBufferStart();
292294 const unsigned char *Cur = Start;
324326 return success();
325327 }
326328
327 error_code IndexedInstrProfReader::getFunctionCounts(
329 std::error_code IndexedInstrProfReader::getFunctionCounts(
328330 StringRef FuncName, uint64_t &FuncHash, std::vector &Counts) {
329331 const auto &Iter = Index->find(FuncName);
330332 if (Iter == Index->end())
339341 return success();
340342 }
341343
342 error_code IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) {
344 std::error_code
345 IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) {
343346 // Are we out of records?
344347 if (RecordIterator == Index->data_end())
345348 return error(instrprof_error::eof);
2727 #include
2828 #endif
2929 using namespace llvm;
30 using std::error_code;
3130
3231 #define DEBUG_TYPE "Data-stream"
3332
6463 return read(Fd, buf, len);
6564 }
6665
67 error_code OpenFile(const std::string &Filename) {
66 std::error_code OpenFile(const std::string &Filename) {
6867 if (Filename == "-") {
6968 Fd = 0;
7069 sys::ChangeStdinToBinary();
71 return error_code();
70 return std::error_code();
7271 }
7372
7473 return sys::fs::openFileForRead(Filename, Fd);
8180 DataStreamer *getDataFileStreamer(const std::string &Filename,
8281 std::string *StrError) {
8382 DataFileStreamer *s = new DataFileStreamer();
84 if (error_code e = s->OpenFile(Filename)) {
83 if (std::error_code e = s->OpenFile(Filename)) {
8584 *StrError = std::string("Could not open ") + Filename + ": " +
8685 e.message() + "\n";
8786 return nullptr;
1616 #include
1717
1818 using llvm::sys::fs::mapped_file_region;
19 using std::error_code;
2019
2120 namespace llvm {
2221 FileOutputBuffer::FileOutputBuffer(mapped_file_region * R,
3029 sys::fs::remove(Twine(TempPath));
3130 }
3231
33 error_code FileOutputBuffer::create(StringRef FilePath,
34 size_t Size,
35 std::unique_ptr &Result,
36 unsigned Flags) {
32 std::error_code
33 FileOutputBuffer::create(StringRef FilePath, size_t Size,
34 std::unique_ptr &Result,
35 unsigned Flags) {
3736 // If file already exists, it must be a regular file (to be mappable).
3837 sys::fs::file_status Stat;
39 error_code EC = sys::fs::status(FilePath, Stat);
38 std::error_code EC = sys::fs::status(FilePath, Stat);
4039 switch (Stat.type()) {
4140 case sys::fs::file_type::file_not_found:
4241 // If file does not exist, we'll create one.
8180 if (Result)
8281 MappedFile.release();
8382
84 return error_code();
83 return std::error_code();
8584 }
8685
87 error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
86 std::error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
8887 // Unmap buffer, letting OS flush dirty pages to file on disk.
8988 Region.reset(nullptr);
9089
9190 // If requested, resize file as part of commit.
9291 if ( NewSmallerSize != -1 ) {
93 error_code EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize);
92 std::error_code EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize);
9493 if (EC)
9594 return EC;
9695 }
2121 #include
2222 #include
2323 using namespace llvm;
24 using std::error_code;
2524
2625 static bool isSignedChar(char C) {
2726 return (C == '+' || C == '-');
177176 // Now its safe to mmap the files into memory because both files
178177 // have a non-zero size.
179178 std::unique_ptr F1;
180 if (error_code ec = MemoryBuffer::getFile(NameA, F1)) {
179 if (std::error_code ec = MemoryBuffer::getFile(NameA, F1)) {
181180 if (Error)
182181 *Error = ec.message();
183182 return 2;
184183 }
185184 std::unique_ptr F2;
186 if (error_code ec = MemoryBuffer::getFile(NameB, F2)) {
185 if (std::error_code ec = MemoryBuffer::getFile(NameB, F2)) {
187186 if (Error)
188187 *Error = ec.message();
189188 return 2;
2121 #include
2222 #endif
2323 using namespace llvm;
24 using std::error_code;
2524
2625 /// \brief Attempt to read the lock file with the given name, if it exists.
2726 ///
7170 LockFileManager::LockFileManager(StringRef FileName)
7271 {
7372 this->FileName = FileName;
74 if (error_code EC = sys::fs::make_absolute(this->FileName)) {
73 if (std::error_code EC = sys::fs::make_absolute(this->FileName)) {
7574 Error = EC;
7675 return;
7776 }
8786 UniqueLockFileName = LockFileName;
8887 UniqueLockFileName += "-%%%%%%%%";
8988 int UniqueLockFileID;
90 if (error_code EC
91 = sys::fs::createUniqueFile(UniqueLockFileName.str(),
92 UniqueLockFileID,
93 UniqueLockFileName)) {
89 if (std::error_code EC = sys::fs::createUniqueFile(
90 UniqueLockFileName.str(), UniqueLockFileID, UniqueLockFileName)) {
9491 Error = EC;
9592 return;
9693 }
122119
123120 while (1) {
124121 // Create a link from the lock file name. If this succeeds, we're done.
125 error_code EC =
122 std::error_code EC =
126123 sys::fs::create_link(UniqueLockFileName.str(), LockFileName.str());
127124 if (!EC)
128125 return;
3232 #include
3333 #endif
3434 using namespace llvm;
35 using std::error_code;
3635
3736 //===----------------------------------------------------------------------===//
3837 // MemoryBuffer implementation itself.
156155 /// if the Filename is "-". If an error occurs, this returns null and fills
157156 /// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
158157 /// returns an empty buffer.
159 error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
160 std::unique_ptr &Result,
161 int64_t FileSize) {
158 std::error_code
159 MemoryBuffer::getFileOrSTDIN(StringRef Filename,
160 std::unique_ptr &Result,
161 int64_t FileSize) {
162162 if (Filename == "-")
163163 return getSTDIN(Result);
164164 return getFile(Filename, Result, FileSize);
190190
191191 public:
192192 MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len,
193 uint64_t Offset, error_code EC)
193 uint64_t Offset, std::error_code EC)
194194 : MFR(FD, false, sys::fs::mapped_file_region::readonly,
195195 getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) {
196196 if (!EC) {
210210 };
211211 }
212212
213 static error_code getMemoryBufferForStream(int FD,
214 StringRef BufferName,
215 std::unique_ptr &Result) {
213 static std::error_code
214 getMemoryBufferForStream(int FD, StringRef BufferName,
215 std::unique_ptr &Result) {
216216 const ssize_t ChunkSize = 4096*4;
217217 SmallString Buffer;
218218 ssize_t ReadBytes;
222222 ReadBytes = read(FD, Buffer.end(), ChunkSize);
223223 if (ReadBytes == -1) {
224224 if (errno == EINTR) continue;
225 return error_code(errno, std::generic_category());
225 return std::error_code(errno, std::generic_category());
226226 }
227227 Buffer.set_size(Buffer.size() + ReadBytes);
228228 } while (ReadBytes != 0);
229229
230230 Result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName));
231 return error_code();
232 }
233
234 static error_code getFileAux(const char *Filename,
235 std::unique_ptr &Result,
236 int64_t FileSize,
237 bool RequiresNullTerminator,
238 bool IsVolatileSize);
239
240 error_code MemoryBuffer::getFile(Twine Filename,
241 std::unique_ptr &Result,
242 int64_t FileSize,
243 bool RequiresNullTerminator,
244 bool IsVolatileSize) {
231 return std::error_code();
232 }
233
234 static std::error_code getFileAux(const char *Filename,
235 std::unique_ptr &Result,
236 int64_t FileSize, bool RequiresNullTerminator,
237 bool IsVolatileSize);
238
239 std::error_code MemoryBuffer::getFile(Twine Filename,
240 std::unique_ptr &Result,
241 int64_t FileSize,
242 bool RequiresNullTerminator,
243 bool IsVolatileSize) {
245244 // Ensure the path is null terminated.
246245 SmallString<256> PathBuf;
247246 StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf);
249248 RequiresNullTerminator, IsVolatileSize);
250249 }
251250
252 static error_code getOpenFileImpl(int FD, const char *Filename,
251 static std::error_code getOpenFileImpl(int FD, const char *Filename,
252 std::unique_ptr &Result,
253 uint64_t FileSize, uint64_t MapSize,
254 int64_t Offset,
255 bool RequiresNullTerminator,
256 bool IsVolatileSize);
257
258 static std::error_code getFileAux(const char *Filename,
253259 std::unique_ptr &Result,
254 uint64_t FileSize, uint64_t MapSize,
255 int64_t Offset, bool RequiresNullTerminator,
256 bool IsVolatileSize);
257
258 static error_code getFileAux(const char *Filename,
259 std::unique_ptr &Result, int64_t FileSize,
260 bool RequiresNullTerminator,
261 bool IsVolatileSize) {
260 int64_t FileSize, bool RequiresNullTerminator,
261 bool IsVolatileSize) {
262262 int FD;
263 error_code EC = sys::fs::openFileForRead(Filename, FD);
263 std::error_code EC = sys::fs::openFileForRead(Filename, FD);
264264 if (EC)
265265 return EC;
266266
267 error_code ret = getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
268 RequiresNullTerminator, IsVolatileSize);
267 std::error_code ret =
268 getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
269 RequiresNullTerminator, IsVolatileSize);
269270 close(FD);
270271 return ret;
271272 }
318319 return true;
319320 }
320321
321 static error_code getOpenFileImpl(int FD, const char *Filename,
322 std::unique_ptr &Result,
323 uint64_t FileSize, uint64_t MapSize,
324 int64_t Offset, bool RequiresNullTerminator,
325 bool IsVolatileSize) {
322 static std::error_code getOpenFileImpl(int FD, const char *Filename,
323 std::unique_ptr &Result,
324 uint64_t FileSize, uint64_t MapSize,
325 int64_t Offset,
326 bool RequiresNullTerminator,
327 bool IsVolatileSize) {
326328 static int PageSize = sys::process::get_self()->page_size();
327329
328330 // Default is to map the full file.
331333 // file descriptor is cheaper than stat on a random path.
332334 if (FileSize == uint64_t(-1)) {
333335 sys::fs::file_status Status;
334 error_code EC = sys::fs::status(FD, Status);
336 std::error_code EC = sys::fs::status(FD, Status);
335337 if (EC)
336338 return EC;
337339
350352
351353 if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
352354 PageSize, IsVolatileSize)) {
353 error_code EC;
355 std::error_code EC;
354356 Result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile(
355357 RequiresNullTerminator, FD, MapSize, Offset, EC));
356358 if (!EC)
357 return error_code();
359 return std::error_code();
358360 }
359361
360362 MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
383385 if (errno == EINTR)
384386 continue;
385387 // Error while reading.
386 return error_code(errno, std::generic_category());
388 return std::error_code(errno, std::generic_category());
387389 }
388390 if (NumRead == 0) {
389391 memset(BufPtr, 0, BytesLeft); // zero-initialize rest of the buffer.
394396 }
395397
396398 Result.swap(SB);
397 return error_code();
398 }
399
400 error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
401 std::unique_ptr &Result,
402 uint64_t FileSize,
403 bool RequiresNullTerminator,
404 bool IsVolatileSize) {
399 return std::error_code();
400 }
401
402 std::error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
403 std::unique_ptr &Result,
404 uint64_t FileSize,
405 bool RequiresNullTerminator,
406 bool IsVolatileSize) {
405407 return getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
406408 RequiresNullTerminator, IsVolatileSize);
407409 }
408410
409 error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
410 std::unique_ptr &Result,
411 uint64_t MapSize, int64_t Offset,
412 bool IsVolatileSize) {
411 std::error_code MemoryBuffer::getOpenFileSlice(
412 int FD, const char *Filename, std::unique_ptr &Result,
413 uint64_t MapSize, int64_t Offset, bool IsVolatileSize) {
413414 return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false,
414415 IsVolatileSize);
415416 }
418419 // MemoryBuffer::getSTDIN implementation.
419420 //===----------------------------------------------------------------------===//
420421
421 error_code MemoryBuffer::getSTDIN(std::unique_ptr &Result) {
422 std::error_code MemoryBuffer::getSTDIN(std::unique_ptr &Result) {
422423 // Read in all of the data from stdin, we cannot mmap stdin.
423424 //
424425 // FIXME: That isn't necessarily true, we should try to mmap stdin and
2727 #endif
2828
2929 using namespace llvm;
30 using std::error_code;
3130
3231 namespace {
3332 using llvm::StringRef;
164163 };
165164
166165 // Implemented in Unix/Path.inc and Windows/Path.inc.
167 static error_code TempDir(SmallVectorImpl &result);
168
169 static error_code createUniqueEntity(const Twine &Model, int &ResultFD,
170 SmallVectorImpl &ResultPath,
171 bool MakeAbsolute, unsigned Mode,
172 FSEntity Type) {
166 static std::error_code TempDir(SmallVectorImpl &result);
167
168 static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD,
169 SmallVectorImpl &ResultPath,
170 bool MakeAbsolute, unsigned Mode,
171 FSEntity Type) {
173172 SmallString<128> ModelStorage;
174173 Model.toVector(ModelStorage);
175174
177176 // Make model absolute by prepending a temp directory if it's not already.
178177 if (!sys::path::is_absolute(Twine(ModelStorage))) {
179178 SmallString<128> TDir;
180 if (error_code EC = TempDir(TDir))
179 if (std::error_code EC = TempDir(TDir))
181180 return EC;
182181 sys::path::append(TDir, Twine(ModelStorage));
183182 ModelStorage.swap(TDir);
201200 // Try to open + create the file.
202201 switch (Type) {
203202 case FS_File: {
204 if (error_code EC =
203 if (std::error_code EC =
205204 sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD,
206205 sys::fs::F_RW | sys::fs::F_Excl, Mode)) {
207206 if (EC == std::errc::file_exists)
209208 return EC;
210209 }
211210
212 return error_code();
211 return std::error_code();
213212 }
214213
215214 case FS_Name: {
216215 bool Exists;
217 error_code EC = sys::fs::exists(ResultPath.begin(), Exists);
216 std::error_code EC = sys::fs::exists(ResultPath.begin(), Exists);
218217 if (EC)
219218 return EC;
220219 if (Exists)
221220 goto retry_random_path;
222 return error_code();
221 return std::error_code();
223222 }
224223
225224 case FS_Dir: {
226 if (error_code EC = sys::fs::create_directory(ResultPath.begin(), false)) {
225 if (std::error_code EC =
226 sys::fs::create_directory(ResultPath.begin(), false)) {
227227 if (EC == std::errc::file_exists)
228228 goto retry_random_path;
229229 return EC;
230230 }
231 return error_code();
231 return std::error_code();
232232 }
233233 }
234234 llvm_unreachable("Invalid Type");
705705
706706 namespace fs {
707707
708 error_code getUniqueID(const Twine Path, UniqueID &Result) {
708 std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
709709 file_status Status;
710 error_code EC = status(Path, Status);
710 std::error_code EC = status(Path, Status);
711711 if (EC)
712712 return EC;
713713 Result = Status.getUniqueID();
714 return error_code();
715 }
716
717 error_code createUniqueFile(const Twine &Model, int &ResultFd,
718 SmallVectorImpl &ResultPath, unsigned Mode) {
714 return std::error_code();
715 }
716
717 std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
718 SmallVectorImpl &ResultPath,
719 unsigned Mode) {
719720 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
720721 }
721722
722 error_code createUniqueFile(const Twine &Model,
723 SmallVectorImpl &ResultPath) {
723 std::error_code createUniqueFile(const Twine &Model,
724 SmallVectorImpl &ResultPath) {
724725 int Dummy;
725726 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
726727 }
727728
728 static error_code createTemporaryFile(const Twine &Model, int &ResultFD,
729 llvm::SmallVectorImpl &ResultPath,
730 FSEntity Type) {
729 static std::error_code
730 createTemporaryFile(const Twine &Model, int &ResultFD,
731 llvm::SmallVectorImpl &ResultPath, FSEntity Type) {
731732 SmallString<128> Storage;
732733 StringRef P = Model.toNullTerminatedStringRef(Storage);
733734 assert(P.find_first_of(separators) == StringRef::npos &&
737738 true, owner_read | owner_write, Type);
738739 }
739740
740 static error_code
741 static std::error_code
741742 createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
742 llvm::SmallVectorImpl &ResultPath,
743 FSEntity Type) {
743 llvm::SmallVectorImpl &ResultPath, FSEntity Type) {
744744 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
745745 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
746746 Type);
747747 }
748748
749
750 error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
751 int &ResultFD,
752 SmallVectorImpl &ResultPath) {
749 std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
750 int &ResultFD,
751 SmallVectorImpl &ResultPath) {
753752 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
754753 }
755754
756 error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
757 SmallVectorImpl &ResultPath) {
755 std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
756 SmallVectorImpl &ResultPath) {
758757 int Dummy;
759758 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
760759 }
762761
763762 // This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
764763 // for consistency. We should try using mkdtemp.
765 error_code createUniqueDirectory(const Twine &Prefix,
766 SmallVectorImpl &ResultPath) {
764 std::error_code createUniqueDirectory(const Twine &Prefix,
765 SmallVectorImpl &ResultPath) {
767766 int Dummy;
768767 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath,
769768 true, 0, FS_Dir);
770769 }
771770
772 error_code make_absolute(SmallVectorImpl &path) {
771 std::error_code make_absolute(SmallVectorImpl &path) {
773772 StringRef p(path.data(), path.size());
774773
775774 bool rootDirectory = path::has_root_directory(p),
781780
782781 // Already absolute.
783782 if (rootName && rootDirectory)
784 return error_code();
783 return std::error_code();
785784
786785 // All of the following conditions will need the current directory.
787786 SmallString<128> current_dir;
788 if (error_code ec = current_path(current_dir)) return ec;
787 if (std::error_code ec = current_path(current_dir))
788 return ec;
789789
790790 // Relative path. Prepend the current directory.
791791 if (!rootName && !rootDirectory) {
793793 path::append(current_dir, p);
794794 // Set path to the result.
795795 path.swap(current_dir);
796 return error_code();
796 return std::error_code();
797797 }
798798
799799 if (!rootName && rootDirectory) {
802802 path::append(curDirRootName, p);
803803 // Set path to the result.
804804 path.swap(curDirRootName);
805 return error_code();
805 return std::error_code();
806806 }
807807
808808 if (rootName && !rootDirectory) {
814814 SmallString<128> res;
815815 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
816816 path.swap(res);
817 return error_code();
817 return std::error_code();
818818 }
819819
820820 llvm_unreachable("All rootName and rootDirectory combinations should have "
821821 "occurred above!");
822822 }
823823
824 error_code create_directories(const Twine &Path, bool IgnoreExisting) {
824 std::error_code create_directories(const Twine &Path, bool IgnoreExisting) {
825825 SmallString<128> PathStorage;
826826 StringRef P = Path.toStringRef(PathStorage);
827827
828828 // Be optimistic and try to create the directory
829 error_code EC = create_directory(P, IgnoreExisting);
829 std::error_code EC = create_directory(P, IgnoreExisting);
830830 // If we succeeded, or had any error other than the parent not existing, just
831831 // return it.
832832 if (EC != std::errc::no_such_file_or_directory)
856856 return status.type() == file_type::directory_file;
857857 }
858858
859 error_code is_directory(const Twine &path, bool &result) {
859 std::error_code is_directory(const Twine &path, bool &result) {
860860 file_status st;
861 if (error_code ec = status(path, st))
861 if (std::error_code ec = status(path, st))
862862 return ec;
863863 result = is_directory(st);
864 return error_code();
864 return std::error_code();
865865 }
866866
867867 bool is_regular_file(file_status status) {
868868 return status.type() == file_type::regular_file;
869869 }
870870
871 error_code is_regular_file(const Twine &path, bool &result) {
871 std::error_code is_regular_file(const Twine &path, bool &result) {
872872 file_status st;
873 if (error_code ec = status(path, st))
873 if (std::error_code ec = status(path, st))
874874 return ec;
875875 result = is_regular_file(st);
876 return error_code();
876 return std::error_code();
877877 }
878878
879879 bool is_other(file_status status) {
10221022 return file_magic::unknown;
10231023 }
10241024
1025 error_code identify_magic(const Twine &Path, file_magic &Result) {
1025 std::error_code identify_magic(const Twine &Path, file_magic &Result) {
10261026 int FD;
1027 if (error_code EC = openFileForRead(Path, FD))
1027 if (std::error_code EC = openFileForRead(Path, FD))
10281028 return EC;
10291029
10301030 char Buffer[32];
10311031 int Length = read(FD, Buffer, sizeof(Buffer));
10321032 if (Length < 0)
1033 return error_code(errno, std::generic_category());
1033 return std::error_code(errno, std::generic_category());
10341034
10351035 Result = identify_magic(StringRef(Buffer, Length));
1036 return error_code();
1037 }
1038
1039 error_code directory_entry::status(file_status &result) const {
1036 return std::error_code();
1037 }
1038
1039 std::error_code directory_entry::status(file_status &result) const {
10401040 return fs::status(Path, result);
10411041 }
10421042
3636 #else
3737 extern "C" void __clear_cache(void *, void*);
3838 #endif
39 using std::error_code;
4039
4140 namespace {
4241
8382 Memory::allocateMappedMemory(size_t NumBytes,
8483 const MemoryBlock *const NearBlock,
8584 unsigned PFlags,
86 error_code &EC) {
87 EC = error_code();
85 std::error_code &EC) {
86 EC = std::error_code();
8887 if (NumBytes == 0)
8988 return MemoryBlock();
9089
9594 #ifdef NEED_DEV_ZERO_FOR_MMAP
9695 static int zero_fd = open("/dev/zero", O_RDWR);
9796 if (zero_fd == -1) {
98 EC = error_code(errno, std::generic_category());
97 EC = std::error_code(errno, std::generic_category());
9998 return MemoryBlock();
10099 }
101100 fd = zero_fd;
123122 if (NearBlock) //Try again without a near hint
124123 return allocateMappedMemory(NumBytes, nullptr, PFlags, EC);
125124
126 EC = error_code(errno, std::generic_category());
125 EC = std::error_code(errno, std::generic_category());
127126 return MemoryBlock();
128127 }
129128
137136 return Result;
138137 }
139138
140 error_code
139 std::error_code
141140 Memory::releaseMappedMemory(MemoryBlock &M) {
142141 if (M.Address == nullptr || M.Size == 0)
143 return error_code();
142 return std::error_code();
144143
145144 if (0 != ::munmap(M.Address, M.Size))
146 return error_code(errno, std::generic_category());
145 return std::error_code(errno, std::generic_category());
147146
148147 M.Address = nullptr;
149148 M.Size = 0;
150149
151 return error_code();
152 }
153
154 error_code
150 return std::error_code();
151 }
152
153 std::error_code
155154 Memory::protectMappedMemory(const MemoryBlock &M, unsigned Flags) {
156155 if (M.Address == nullptr || M.Size == 0)
157 return error_code();
156 return std::error_code();
158157
159158 if (!Flags)
160 return error_code(EINVAL, std::generic_category());
159 return std::error_code(EINVAL, std::generic_category());
161160
162161 int Protect = getPosixProtectionFlags(Flags);
163162
164163 int Result = ::mprotect(M.Address, M.Size, Protect);
165164 if (Result != 0)
166 return error_code(errno, std::generic_category());
165 return std::error_code(errno, std::generic_category());
167166
168167 if (Flags & MF_EXEC)
169168 Memory::InvalidateInstructionCache(M.Address, M.Size);
170169
171 return error_code();
170 return std::error_code();
172171 }
173172
174173 /// AllocateRWX - Allocate a slab of memory with read/write/execute
8686 };
8787 }
8888
89 static error_code TempDir(SmallVectorImpl &result) {
89 static std::error_code TempDir(SmallVectorImpl &result) {
9090 // FIXME: Don't use TMPDIR if program is SUID or SGID enabled.
9191 const char *dir = nullptr;
9292 (dir = std::getenv("TMPDIR")) || (dir = std::getenv("TMP")) ||
9999 result.clear();
100100 StringRef d(dir);
101101 result.append(d.begin(), d.end());
102 return error_code();
102 return std::error_code();
103103 }
104104
105105 namespace llvm {
224224 return UniqueID(fs_st_dev, fs_st_ino);
225225 }
226226
227 error_code current_path(SmallVectorImpl &result) {
227 std::error_code current_path(SmallVectorImpl &result) {
228228 result.clear();
229229
230230 const char *pwd = ::getenv("PWD");
234234 !llvm::sys::fs::status(".", DotStatus) &&
235235 PWDStatus.getUniqueID() == DotStatus.getUniqueID()) {
236236 result.append(pwd, pwd + strlen(pwd));
237 return error_code();
237 return std::error_code();
238238 }
239239
240240 #ifdef MAXPATHLEN
248248 if (::getcwd(result.data(), result.capacity()) == nullptr) {
249249 // See if there was a real error.
250250 if (errno != ENOMEM)
251 return error_code(errno, std::generic_category());
251 return std::error_code(errno, std::generic_category());
252252 // Otherwise there just wasn't enough space.
253253 result.reserve(result.capacity() * 2);
254254 } else
256256 }
257257
258258 result.set_size(strlen(result.data()));
259 return error_code();
260 }
261
262 error_code create_directory(const Twine &path, bool IgnoreExisting) {
259 return std::error_code();
260 }
261
262 std::error_code create_directory(const Twine &path, bool IgnoreExisting) {
263263 SmallString<128> path_storage;
264264 StringRef p = path.toNullTerminatedStringRef(path_storage);
265265
266266 if (::mkdir(p.begin(), S_IRWXU | S_IRWXG) == -1) {
267267 if (errno != EEXIST || !IgnoreExisting)
268 return error_code(errno, std::generic_category());
269 }
270
271 return error_code();
272 }
273
274 error_code normalize_separators(SmallVectorImpl &Path) {
268 return std::error_code(errno, std::generic_category());
269 }
270
271 return std::error_code();
272 }
273
274 std::error_code normalize_separators(SmallVectorImpl &Path) {
275275 for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) {
276276 if (*PI == '\\') {
277277 auto PN = PI + 1;
281281 *PI = '/';
282282 }
283283 }
284 return error_code();
284 return std::error_code();
285285 }
286286
287287 // Note that we are using symbolic link because hard links are not supported by
288288 // all filesystems (SMB doesn't).
289 error_code create_link(const Twine &to, const Twine &from) {
289 std::error_code create_link(const Twine &to, const Twine &from) {
290290 // Get arguments.
291291 SmallString<128> from_storage;
292292 SmallString<128> to_storage;
294294 StringRef t = to.toNullTerminatedStringRef(to_storage);
295295
296296 if (::symlink(t.begin(), f.begin()) == -1)
297 return error_code(errno, std::generic_category());
298
299 return error_code();
300 }
301
302 error_code remove(const Twine &path, bool IgnoreNonExisting) {
297 return std::error_code(errno, std::generic_category());
298
299 return std::error_code();
300 }
301
302 std::error_code remove(const Twine &path, bool IgnoreNonExisting) {
303303 SmallString<128> path_storage;
304304 StringRef p = path.toNullTerminatedStringRef(path_storage);
305305
306306 struct stat buf;
307307 if (lstat(p.begin(), &buf) != 0) {
308308 if (errno != ENOENT || !IgnoreNonExisting)
309 return error_code(errno, std::generic_category());
310 return error_code();
309 return std::error_code(errno, std::generic_category());
310 return std::error_code();
311311 }
312312
313313 // Note: this check catches strange situations. In all cases, LLVM should
320320
321321 if (::remove(p.begin()) == -1) {
322322 if (errno != ENOENT || !IgnoreNonExisting)
323 return error_code(errno, std::generic_category());
324 }
325
326 return error_code();
327 }
328
329 error_code rename(const Twine &from, const Twine &to) {
323 return std::error_code(errno, std::generic_category());
324 }
325
326 return std::error_code();
327 }
328
329 std::error_code rename(const Twine &from, const Twine &to) {
330330 // Get arguments.
331331 SmallString<128> from_storage;
332332 SmallString<128> to_storage;
334334 StringRef t = to.toNullTerminatedStringRef(to_storage);
335335
336336 if (::rename(f.begin(), t.begin()) == -1)
337 return error_code(errno, std::generic_category());
338
339 return error_code();
340 }
341
342 error_code resize_file(const Twine &path, uint64_t size) {
337 return std::error_code(errno, std::generic_category());
338
339 return std::error_code();
340 }
341
342 std::error_code resize_file(const Twine &path, uint64_t size) {
343343 SmallString<128> path_storage;
344344 StringRef p = path.toNullTerminatedStringRef(path_storage);
345345
346346 if (::truncate(p.begin(), size) == -1)
347 return error_code(errno, std::generic_category());
348
349 return error_code();
350 }
351
352 error_code exists(const Twine &path, bool &result) {
347 return std::error_code(errno, std::generic_category());
348
349 return std::error_code();
350 }
351
352 std::error_code exists(const Twine &path, bool &result) {
353353 SmallString<128> path_storage;
354354 StringRef p = path.toNullTerminatedStringRef(path_storage);
355355
356356 if (::access(p.begin(), F_OK) == -1) {
357357 if (errno != ENOENT)
358 return error_code(errno, std::generic_category());
358 return std::error_code(errno, std::generic_category());
359359 result = false;
360360 } else
361361 result = true;
362362
363 return error_code();
363 return std::error_code();
364364 }
365365
366366 bool can_write(const Twine &Path) {
389389 A.fs_st_ino == B.fs_st_ino;
390390 }
391391
392 error_code equivalent(const Twine &A, const Twine &B, bool &result) {
392 std::error_code equivalent(const Twine &A, const Twine &B, bool &result) {
393393 file_status fsA, fsB;
394 if (error_code ec = status(A, fsA)) return ec;
395 if (error_code ec = status(B, fsB)) return ec;
394 if (std::error_code ec = status(A, fsA))
395 return ec;
396 if (std::error_code ec = status(B, fsB))
397 return ec;
396398 result = equivalent(fsA, fsB);
397 return error_code();
398 }
399
400 static error_code fillStatus(int StatRet, const struct stat &Status,
399 return std::error_code();
400 }
401
402 static std::error_code fillStatus(int StatRet, const struct stat &Status,
401403 file_status &Result) {
402404 if (StatRet != 0) {
403 error_code ec(errno, std::generic_category());
405 std::error_code ec(errno, std::generic_category());
404406 if (ec == std::errc::no_such_file_or_directory)
405407 Result = file_status(file_type::file_not_found);
406408 else
428430 file_status(Type, Perms, Status.st_dev, Status.st_ino, Status.st_mtime,
429431 Status.st_uid, Status.st_gid, Status.st_size);
430432
431 return error_code();
432 }
433
434 error_code status(const Twine &Path, file_status &Result) {
433 return std::error_code();
434 }
435
436 std::error_code status(const Twine &Path, file_status &Result) {
435437 SmallString<128> PathStorage;
436438 StringRef P = Path.toNullTerminatedStringRef(PathStorage);
437439
440442 return fillStatus(StatRet, Status, Result);
441443 }
442444
443 error_code status(int FD, file_status &Result) {
445 std::error_code status(int FD, file_status &Result) {
444446 struct stat Status;
445447 int StatRet = ::fstat(FD, &Status);
446448 return fillStatus(StatRet, Status, Result);
447449 }
448450
449 error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
451 std::error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
450452 #if defined(HAVE_FUTIMENS)
451453 timespec Times[2];
452454 Times[0].tv_sec = Time.toEpochTime();
453455 Times[0].tv_nsec = 0;
454456 Times[1] = Times[0];
455457 if (::futimens(FD, Times))
456 return error_code(errno, std::generic_category());
457 return error_code();
458 return std::error_code(errno, std::generic_category());
459 return std::error_code();
458460 #elif defined(HAVE_FUTIMES)
459461 timeval Times[2];
460462 Times[0].tv_sec = Time.toEpochTime();
461463 Times[0].tv_usec = 0;
462464 Times[1] = Times[0];
463465 if (::futimes(FD, Times))
464 return error_code(errno, std::generic_category());
465 return error_code();
466 return std::error_code(errno, std::generic_category());
467 return std::error_code();
466468 #else
467469 #warning Missing futimes() and futimens()
468470 return make_error_code(std::errc::not_supported);
469471 #endif
470472 }
471473
472 error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) {
474 std::error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) {
473475 AutoFD ScopedFD(FD);
474476 if (!CloseFD)
475477 ScopedFD.take();
477479 // Figure out how large the file is.
478480 struct stat FileInfo;
479481 if (fstat(FD, &FileInfo) == -1)
480 return error_code(errno, std::generic_category());
482 return std::error_code(errno, std::generic_category());
481483 uint64_t FileSize = FileInfo.st_size;
482484
483485 if (Size == 0)
485487 else if (FileSize < Size) {
486488 // We need to grow the file.
487489 if (ftruncate(FD, Size) == -1)
488 return error_code(errno, std::generic_category());
490 return std::error_code(errno, std::generic_category());
489491 }
490492
491493 int flags = (Mode == readwrite) ? MAP_SHARED : MAP_PRIVATE;
495497 #endif
496498 Mapping = ::mmap(nullptr, Size, prot, flags, FD, Offset);
497499 if (Mapping == MAP_FAILED)
498 return error_code(errno, std::generic_category());
499 return error_code();
500 return std::error_code(errno, std::generic_category());
501 return std::error_code();
500502 }
501503
502504 mapped_file_region::mapped_file_region(const Twine &path,
503505 mapmode mode,
504506 uint64_t length,
505507 uint64_t offset,
506 error_code &ec)
508 std::error_code &ec)
507509 : Mode(mode)
508510 , Size(length)
509511 , Mapping() {
518520 int oflags = (mode == readonly) ? O_RDONLY : O_RDWR;
519521 int ofd = ::open(name.begin(), oflags);
520522 if (ofd == -1) {
521 ec = error_code(errno, std::generic_category());
523 ec = std::error_code(errno, std::generic_category());
522524 return;
523525 }
524526
532534 mapmode mode,
533535 uint64_t length,
534536 uint64_t offset,
535 error_code &ec)
537 std::error_code &ec)
536538 : Mode(mode)
537539 , Size(length)
538540 , Mapping() {
582584 return process::get_self()->page_size();
583585 }
584586
585 error_code detail::directory_iterator_construct(detail::DirIterState &it,
587 std::error_code detail::directory_iterator_construct(detail::DirIterState &it,
586588 StringRef path){
587589 SmallString<128> path_null(path);
588590 DIR *directory = ::opendir(path_null.c_str());
589591 if (!directory)
590 return error_code(errno, std::generic_category());
592 return std::error_code(errno, std::generic_category());
591593
592594 it.IterationHandle = reinterpret_cast(directory);
593595 // Add something for replace_filename to replace.
596598 return directory_iterator_increment(it);
597599 }
598600
599 error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
601 std::error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
600602 if (it.IterationHandle)
601603 ::closedir(reinterpret_cast(it.IterationHandle));
602604 it.IterationHandle = 0;
603605 it.CurrentEntry = directory_entry();
604 return error_code();
605 }
606
607 error_code detail::directory_iterator_increment(detail::DirIterState &it) {
606 return std::error_code();
607 }
608
609 std::error_code detail::directory_iterator_increment(detail::DirIterState &it) {
608610 errno = 0;
609611 dirent *cur_dir = ::readdir(reinterpret_cast(it.IterationHandle));
610612 if (cur_dir == nullptr && errno != 0) {
611 return error_code(errno, std::generic_category());
613 return std::error_code(errno, std::generic_category());
612614 } else if (cur_dir != nullptr) {
613615