LLVM: lib/ObjCopy/COFF/COFFObjcopy.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
15
22#include
23
24namespace llvm {
26namespace coff {
27
28using namespace object;
29using namespace COFF;
30
34
36 if (Obj.getSections().empty())
37 return 0;
38 const Section &Last = Obj.getSections().back();
39 return alignTo(Last.Header.VirtualAddress + Last.Header.VirtualSize,
40 Obj.IsPE ? Obj.PeHeader.SectionAlignment : 1);
41}
42
47 if (!LinkTargetOrErr)
49 auto LinkTarget = std::move(*LinkTargetOrErr);
51
53 size_t CRCPos = alignTo(FileName.size() + 1, 4);
54 std::vector<uint8_t> Data(CRCPos + 4);
55 memcpy(Data.data(), FileName.data(), FileName.size());
58}
59
60
65
68 Sec.Name = Name;
69 Sec.Header.VirtualSize = NeedVA ? Sec.getContents().size() : 0u;
70 Sec.Header.VirtualAddress = NeedVA ? getNextRVA(Obj) : 0u;
71 Sec.Header.SizeOfRawData =
72 NeedVA ? alignTo(Sec.Header.VirtualSize,
73 Obj.IsPE ? Obj.PeHeader.FileAlignment : 1)
74 : Sec.getContents().size();
75
76 Sec.Header.PointerToRelocations = 0;
77 Sec.Header.PointerToLinenumbers = 0;
78
79 Sec.Header.NumberOfLinenumbers = 0;
81
82 Obj.addSections(Sec);
83}
84
88 if (!Contents)
90
91 addSection(Obj, ".gnu_debuglink", *Contents,
94
96}
97
99
100 const uint32_t PreserveMask =
108
109
110
112
120 NewCharacteristics |=
130
131 return NewCharacteristics;
132}
133
137 continue;
138
140
141 std::unique_ptr Buffer;
143 Buffer = std::move(*B);
144 else
145 return B.takeError();
146
147 llvm::copy(Contents, Buffer->getBufferStart());
148 if (Error E = Buffer->commit())
149 return E;
150
152 }
153 return createStringError(object_error::parse_failed, "section '%s' not found",
155}
156
162 return E;
163 }
164
165
166 Obj.removeSections([&Config](const Section &Sec) {
167
168
170 return true;
171
176 return true;
177 }
178
180 return true;
181
182 return false;
183 });
184
189 : nullptr;
190
191
192 Obj.truncateSections([DebugDir](const Section &Sec) {
194 !(DebugDir && DebugDir->Size > 0 &&
200 });
201 }
202
203
205 for (Section &Sec : Obj.getMutableSections())
207
208
211 if (Error E = Obj.markSymbols())
212 return E;
213
214 for (Symbol &Sym : Obj.getMutableSymbols()) {
217 Sym.Name = I->getValue();
218 }
219
221
222
224 return true;
225
227
228 if (Sym.Referenced)
231 "'" + Config.OutputFilename + "': not stripping symbol '" +
232 Sym.Name.str() + "' because it is named in a relocation");
233 return true;
234 }
235
236 if (!Sym.Referenced) {
237
238
239
240
242 Sym.Sym.SectionNumber == 0)
245 return true;
246
247
248
249
252 Sym.Sym.SectionNumber != 0)
253 return true;
254 }
255
256 return false;
257 };
258
259
261 return Err;
262
264 for (Section &Sec : Obj.getMutableSections()) {
269 }
270
276 else
278
281 NewSection.SectionData->getBufferStart()),
282 NewSection.SectionData->getBufferSize()),
284 }
285
287 auto It = llvm::find_if(Obj.getMutableSections(), [&](auto &Sec) {
288 return Sec.Name == NewSection.SectionName;
289 });
290 if (It == Obj.getMutableSections().end())
292 "could not find section with name '%s'",
294 size_t ContentSize = It->getContents().size();
295 if (!ContentSize)
298 "section '%s' cannot be updated because it does not have contents",
300 if (ContentSize < NewSection.SectionData->getBufferSize())
303 "new section cannot be larger than previous section");
304 It->setOwnedContents({NewSection.SectionData->getBufferStart(),
305 NewSection.SectionData->getBufferEnd()});
306 }
307
310 return E;
311
314 if (!Obj.IsPE)
318 "': unable to set subsystem on a relocatable object file");
325 }
326
328}
329
335 if (!ObjOrErr)
338 assert(Obj && "Unable to deserialize COFF object");
342 if (Error E = Writer.write())
345}
346
347}
348}
349}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
ReachingDefInfo InstSet & ToRemove
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
Represents either an error or a value T.
std::error_code getError() const
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
static LLVM_ABI Expected< std::unique_ptr< FileOutputBuffer > > create(StringRef FilePath, size_t Size, unsigned Flags=0)
Factory method to create an OutputBuffer object which manages a read/write buffer of the specified si...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
constexpr bool empty() const
empty - Check if the string is empty.
constexpr size_t size() const
size - Get the string size.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
bool matches(StringRef S) const
Expected< std::unique_ptr< Object > > create() const
This class implements an extremely fast bulk output stream that can only output to a stream.
@ IMAGE_SCN_ALIGN_64BYTES
@ IMAGE_SCN_ALIGN_128BYTES
@ IMAGE_SCN_ALIGN_256BYTES
@ IMAGE_SCN_ALIGN_1024BYTES
@ IMAGE_SCN_ALIGN_512BYTES
@ IMAGE_SCN_CNT_UNINITIALIZED_DATA
@ IMAGE_SCN_MEM_DISCARDABLE
@ IMAGE_SCN_ALIGN_4096BYTES
@ IMAGE_SCN_CNT_INITIALIZED_DATA
@ IMAGE_SCN_ALIGN_8192BYTES
@ IMAGE_SCN_ALIGN_16BYTES
@ IMAGE_SCN_ALIGN_32BYTES
@ IMAGE_SCN_ALIGN_2048BYTES
@ IMAGE_SYM_CLASS_STATIC
Static.
static bool isDebugSection(const Section &Sec)
Definition COFFObjcopy.cpp:31
LLVM_ABI Error executeObjcopyOnBinary(const CommonConfig &Config, const COFFConfig &, object::COFFObjectFile &In, raw_ostream &Out)
Apply the transformations described by Config and COFFConfig to In and writes the result into Out.
Definition COFFObjcopy.cpp:330
static void addSection(Object &Obj, StringRef Name, ArrayRef< uint8_t > Contents, uint32_t Characteristics)
Definition COFFObjcopy.cpp:61
static uint32_t flagsToCharacteristics(SectionFlag AllFlags, uint32_t OldChar)
Definition COFFObjcopy.cpp:98
static Error dumpSection(Object &O, StringRef SectionName, StringRef FileName)
Definition COFFObjcopy.cpp:134
static uint64_t getNextRVA(const Object &Obj)
Definition COFFObjcopy.cpp:35
static Error handleArgs(const CommonConfig &Config, const COFFConfig &COFFConfig, Object &Obj)
Definition COFFObjcopy.cpp:157
static Error addGnuDebugLink(Object &Obj, StringRef DebugLinkFile)
Definition COFFObjcopy.cpp:85
static Expected< std::vector< uint8_t > > createGnuDebugLinkSectionContents(StringRef File)
Definition COFFObjcopy.cpp:44
void write32le(void *P, uint32_t V)
LLVM_ABI StringRef filename(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get filename.
This is an optimization pass for GlobalISel generic memory operations.
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
LLVM_ABI uint32_t crc32(ArrayRef< uint8_t > Data)
FunctionAddr VTableAddr uintptr_t uintptr_t Data
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
DWARFExpression::Operation Op
OutputIt copy(R &&Range, OutputIt Out)
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
std::optional< unsigned > MinorSubsystemVersion
std::optional< unsigned > Subsystem
std::optional< unsigned > MajorSubsystemVersion
SmallVector< NewSectionInfo, 0 > UpdateSection
NameMatcher SymbolsToRemove
SmallVector< StringRef, 0 > DumpSection
StringRef AddGnuDebugLink
StringMap< SectionFlagsUpdate > SetSectionFlags
SmallVector< NewSectionInfo, 0 > AddSection
NameMatcher UnneededSymbolsToRemove
StringMap< StringRef > SymbolsToRename
std::shared_ptr< MemoryBuffer > SectionData
void setOwnedContents(std::vector< uint8_t > &&Data)
object::coff_section Header
std::vector< Relocation > Relocs
ArrayRef< uint8_t > getContents() const
support::ulittle32_t Characteristics
support::ulittle32_t SizeOfRawData
support::ulittle32_t VirtualAddress
support::ulittle32_t RelativeVirtualAddress
support::ulittle32_t Size