xref: /llvm-project/llvm/tools/llvm-jitlink/llvm-jitlink-elf.cpp (revision fc36a511c66702e1bbbfeac701423f18ad4e137e)
1 //===---- llvm-jitlink-elf.cpp -- ELF 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 // ELF 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 isELFGOTSection(Section &S) { return S.getName() == "$__GOT"; }
24 
25 static bool isELFStubsSection(Section &S) { return S.getName() == "$__STUBS"; }
26 
27 static Expected<Edge &> getFirstRelocationEdge(LinkGraph &G, Block &B) {
28   auto EItr = std::find_if(B.edges().begin(), B.edges().end(),
29                            [](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 &> getELFGOTTarget(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 &> getELFStubTarget(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() || !isELFGOTSection(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 getELFGOTTarget(G, GOTSym.getBlock());
65 }
66 
67 namespace llvm {
68 
69 Error registerELFGraphInfo(Session &S, LinkGraph &G) {
70   auto FileName = sys::path::filename(G.getName());
71   if (S.FileInfos.count(FileName)) {
72     return make_error<StringError>("When -check is passed, file names must be "
73                                    "distinct (duplicate: \"" +
74                                        FileName + "\")",
75                                    inconvertibleErrorCode());
76   }
77 
78   auto &FileInfo = S.FileInfos[FileName];
79   LLVM_DEBUG({
80     dbgs() << "Registering ELF file info for \"" << FileName << "\"\n";
81   });
82   for (auto &Sec : G.sections()) {
83     LLVM_DEBUG({
84       dbgs() << "  Section \"" << Sec.getName() << "\": "
85              << (llvm::empty(Sec.symbols()) ? "empty. skipping."
86                                             : "processing...")
87              << "\n";
88     });
89 
90     // Skip empty sections.
91     if (llvm::empty(Sec.symbols()))
92       continue;
93 
94     if (FileInfo.SectionInfos.count(Sec.getName()))
95       return make_error<StringError>("Encountered duplicate section name \"" +
96                                          Sec.getName() + "\" in \"" + FileName +
97                                          "\"",
98                                      inconvertibleErrorCode());
99 
100     bool isGOTSection = isELFGOTSection(Sec);
101     bool isStubsSection = isELFStubsSection(Sec);
102 
103     bool SectionContainsContent = false;
104     bool SectionContainsZeroFill = false;
105 
106     auto *FirstSym = *Sec.symbols().begin();
107     auto *LastSym = FirstSym;
108     for (auto *Sym : Sec.symbols()) {
109       if (Sym->getAddress() < FirstSym->getAddress())
110         FirstSym = Sym;
111       if (Sym->getAddress() > LastSym->getAddress())
112         LastSym = Sym;
113 
114       if (isGOTSection) {
115         if (Sym->isSymbolZeroFill())
116           return make_error<StringError>("zero-fill atom in GOT section",
117                                          inconvertibleErrorCode());
118 
119         if (auto TS = getELFGOTTarget(G, Sym->getBlock()))
120           FileInfo.GOTEntryInfos[TS->getName()] = {Sym->getSymbolContent(),
121                                                    Sym->getAddress()};
122         else
123           return TS.takeError();
124         SectionContainsContent = true;
125       } else if (isStubsSection) {
126         if (Sym->isSymbolZeroFill())
127           return make_error<StringError>("zero-fill atom in Stub section",
128                                          inconvertibleErrorCode());
129 
130         if (auto TS = getELFStubTarget(G, Sym->getBlock()))
131           FileInfo.StubInfos[TS->getName()] = {Sym->getSymbolContent(),
132                                                Sym->getAddress()};
133         else
134           return TS.takeError();
135         SectionContainsContent = true;
136       }
137 
138       if (Sym->hasName()) {
139         if (Sym->isSymbolZeroFill()) {
140           S.SymbolInfos[Sym->getName()] = {Sym->getSize(), Sym->getAddress()};
141           SectionContainsZeroFill = true;
142         } else {
143           S.SymbolInfos[Sym->getName()] = {Sym->getSymbolContent(),
144                                            Sym->getAddress()};
145           SectionContainsContent = true;
146         }
147       }
148     }
149 
150     JITTargetAddress SecAddr = FirstSym->getAddress();
151     uint64_t SecSize =
152         (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) -
153         SecAddr;
154 
155     if (SectionContainsZeroFill && SectionContainsContent)
156       return make_error<StringError>("Mixed zero-fill and content sections not "
157                                      "supported yet",
158                                      inconvertibleErrorCode());
159     if (SectionContainsZeroFill)
160       FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr};
161     else
162       FileInfo.SectionInfos[Sec.getName()] = {
163           StringRef(FirstSym->getBlock().getContent().data(), SecSize),
164           SecAddr};
165   }
166 
167   return Error::success();
168 }
169 
170 } // end namespace llvm
171