xref: /llvm-project/llvm/tools/llvm-jitlink/llvm-jitlink-coff.cpp (revision 096551537b2a747a3387726ca618ceeb3950e9bc)
1 //===--- llvm-jitlink-coff.cpp -- COFF parsing support for llvm-jitlink ---===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // COFF parsing support for llvm-jitlink.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm-jitlink.h"
14 
15 #include "llvm/Support/Error.h"
16 #include "llvm/Support/Path.h"
17 
18 #define DEBUG_TYPE "llvm_jitlink"
19 
20 using namespace llvm;
21 using namespace llvm::jitlink;
22 
23 static bool isCOFFGOTSection(Section &S) { return S.getName() == "$__GOT"; }
24 
25 static bool isCOFFStubsSection(Section &S) { return S.getName() == "$__STUBS"; }
26 
27 static Expected<Edge &> getFirstRelocationEdge(LinkGraph &G, Block &B) {
28   auto EItr =
29       llvm::find_if(B.edges(), [](Edge &E) { return E.isRelocation(); });
30   if (EItr == B.edges().end())
31     return make_error<StringError>("GOT entry in " + G.getName() + ", \"" +
32                                        B.getSection().getName() +
33                                        "\" has no relocations",
34                                    inconvertibleErrorCode());
35   return *EItr;
36 }
37 
38 static Expected<Symbol &> getCOFFGOTTarget(LinkGraph &G, Block &B) {
39   auto E = getFirstRelocationEdge(G, B);
40   if (!E)
41     return E.takeError();
42   auto &TargetSym = E->getTarget();
43   if (!TargetSym.hasName())
44     return make_error<StringError>(
45         "GOT entry in " + G.getName() + ", \"" +
46             TargetSym.getBlock().getSection().getName() +
47             "\" points to anonymous "
48             "symbol",
49         inconvertibleErrorCode());
50   return TargetSym;
51 }
52 
53 static Expected<Symbol &> getCOFFStubTarget(LinkGraph &G, Block &B) {
54   auto E = getFirstRelocationEdge(G, B);
55   if (!E)
56     return E.takeError();
57   auto &GOTSym = E->getTarget();
58   if (!GOTSym.isDefined() || !isCOFFGOTSection(GOTSym.getBlock().getSection()))
59     return make_error<StringError>(
60         "Stubs entry in " + G.getName() + ", \"" +
61             GOTSym.getBlock().getSection().getName() +
62             "\" does not point to GOT entry",
63         inconvertibleErrorCode());
64   return getCOFFGOTTarget(G, GOTSym.getBlock());
65 }
66 
67 namespace llvm {
68 Error registerCOFFGraphInfo(Session &S, LinkGraph &G) {
69   std::lock_guard<std::mutex> Lock(S.M);
70 
71   auto FileName = sys::path::filename(G.getName());
72   if (S.FileInfos.count(FileName)) {
73     return make_error<StringError>("When -check is passed, file names must be "
74                                    "distinct (duplicate: \"" +
75                                        FileName + "\")",
76                                    inconvertibleErrorCode());
77   }
78 
79   auto &FileInfo = S.FileInfos[FileName];
80   LLVM_DEBUG(
81       { dbgs() << "Registering COFF file info for \"" << FileName << "\"\n"; });
82   for (auto &Sec : G.sections()) {
83     LLVM_DEBUG({
84       dbgs() << "  Section \"" << Sec.getName() << "\": "
85              << (Sec.symbols().empty() ? "empty. skipping." : "processing...")
86              << "\n";
87     });
88 
89     // Skip empty sections.
90     if (Sec.symbols().empty())
91       continue;
92 
93     if (FileInfo.SectionInfos.count(Sec.getName()))
94       return make_error<StringError>("Encountered duplicate section name \"" +
95                                          Sec.getName() + "\" in \"" + FileName +
96                                          "\"",
97                                      inconvertibleErrorCode());
98 
99     bool isGOTSection = isCOFFGOTSection(Sec);
100     bool isStubsSection = isCOFFStubsSection(Sec);
101 
102     bool SectionContainsContent = false;
103     bool SectionContainsZeroFill = false;
104 
105     auto *FirstSym = *Sec.symbols().begin();
106     auto *LastSym = FirstSym;
107     for (auto *Sym : Sec.symbols()) {
108       if (Sym->getAddress() < FirstSym->getAddress())
109         FirstSym = Sym;
110       if (Sym->getAddress() > LastSym->getAddress())
111         LastSym = Sym;
112 
113       if (isGOTSection || isStubsSection) {
114         if (isGOTSection) {
115           // Skip the GOT start symbol
116           if (Sym->getSize() != 0)
117             if (Error E = FileInfo.registerGOTEntry(G, *Sym, getCOFFGOTTarget))
118               return E;
119         } else {
120           if (Error E = FileInfo.registerStubEntry(G, *Sym, getCOFFStubTarget))
121             return E;
122         }
123         SectionContainsContent = true;
124       }
125 
126       if (Sym->hasName()) {
127         if (Sym->isSymbolZeroFill()) {
128           S.SymbolInfos[Sym->getName()] = {Sym->getSize(),
129                                            Sym->getAddress().getValue()};
130           SectionContainsZeroFill = true;
131         } else {
132           S.SymbolInfos[Sym->getName()] = {Sym->getSymbolContent(),
133                                            Sym->getAddress().getValue(),
134                                            Sym->getTargetFlags()};
135           SectionContainsContent = true;
136         }
137       }
138     }
139 
140     auto SecAddr = FirstSym->getAddress();
141     auto SecSize =
142         (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) -
143         SecAddr;
144 
145     if (SectionContainsZeroFill && SectionContainsContent)
146       return make_error<StringError>("Mixed zero-fill and content sections not "
147                                      "supported yet",
148                                      inconvertibleErrorCode());
149 
150     if (SectionContainsZeroFill)
151       FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr.getValue()};
152     else
153       FileInfo.SectionInfos[Sec.getName()] = {
154           ArrayRef<char>(FirstSym->getBlock().getContent().data(), SecSize),
155           SecAddr.getValue(), FirstSym->getTargetFlags()};
156   }
157 
158   return Error::success();
159 }
160 
161 } // end namespace llvm
162