/
llvm-objcopy.cpp
288 lines (244 loc) · 9.55 KB
/
llvm-objcopy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//===-- llvm-objcopy.cpp - Object file copying utility for llvm -----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This program is a utility that mimics a tiny subset of binutils "objcopy".
//
//===----------------------------------------------------------------------===//
#include "llvm-objcopy.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/COFF.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/system_error.h"
using namespace llvm;
using namespace object;
static cl::opt<std::string>
InputFilename(cl::Positional, cl::desc("<input object file>"), cl::Required);
static cl::opt<std::string>
OutputFilename(cl::Positional, cl::desc("<output object file>"), cl::Required);
namespace {
enum OutputFormatTy { binary, intel_hex, readmemh };
cl::opt<OutputFormatTy>
OutputTarget("O",
cl::desc("Specify output target"),
cl::values(clEnumVal(binary, "raw binary"),
clEnumVal(intel_hex, "Intel Hex format"),
clEnumVal(readmemh, "Format read by Verilog's $readmemh system task"),
clEnumValEnd),
cl::init(binary));
cl::alias OutputTarget2("output-target", cl::desc("Alias for -O"),
cl::aliasopt(OutputTarget));
static StringRef ToolName;
}
bool llvm::error(error_code ec) {
if (!ec) return false;
errs() << ToolName << ": error reading file: " << ec.message() << ".\n";
return true;
}
class ObjectCopyBase {
public:
ObjectCopyBase(StringRef InputFilename)
: mBinaryOutput(false)
, mFillGaps(false)
{
}
virtual ~ObjectCopyBase() {}
void CopyTo(ObjectFile *o, StringRef OutputFilename) const {
if (o == NULL) {
return;
}
std::string ErrorInfo;
tool_output_file Out(OutputFilename.data(), ErrorInfo, mBinaryOutput ? sys::fs::F_Binary : sys::fs::F_None);
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
return;
}
error_code ec;
bool FillNextGap = false;
uint64_t LastAddress;
StringRef LastSectionName;
for (section_iterator si = o->begin_sections(), se = o->end_sections(); si != se; si.increment(ec)) {
if (error(ec)) return;
StringRef SectionName;
StringRef SectionContents;
uint64_t SectionAddress;
bool BSS;
bool Required;
if (error(si->getName(SectionName))) return;
if (error(si->getContents(SectionContents))) return;
if (error(si->getAddress(SectionAddress))) return;
if (error(si->isBSS(BSS))) continue;
if (error(si->isRequiredForExecution(Required))) continue;
if ( !Required
|| BSS
|| SectionContents.size() == 0) {
continue;
}
if (FillNextGap) {
if (SectionAddress < LastAddress) {
errs() << "Trying to fill gaps between sections " << LastSectionName << " and " << SectionName << " in invalid order\n";
return;
} else if (SectionAddress == LastAddress) {
// No gap, do nothing
} else if (SectionAddress - LastAddress > (1<<16)) {
// Gap size limit reached
errs() << "Gap between sections is too large\n";
return;
} else {
FillGap(Out, 0x00, SectionAddress - LastAddress);
}
}
PrintSection(Out, SectionName, SectionContents, SectionAddress);
if (mFillGaps) {
FillNextGap = true;
LastSectionName = SectionName;
LastAddress = SectionAddress + SectionContents.size();
}
}
Out.keep();
}
protected:
virtual void PrintSection(tool_output_file &Out, const StringRef &SectionName,
const StringRef &SectionContents, uint64_t SectionAddress) const = 0;
virtual void FillGap(tool_output_file &Out, unsigned char Value, uint64_t Size) const { }
bool mBinaryOutput;
bool mFillGaps;
};
class ObjectCopyIntelHex : public ObjectCopyBase {
public:
ObjectCopyIntelHex(StringRef InputFilename)
: ObjectCopyBase(InputFilename) {}
virtual ~ObjectCopyIntelHex() {}
protected:
virtual void PrintSection(tool_output_file &Out, const StringRef &SectionName,
const StringRef &SectionContents, uint64_t SectionAddress) const
{
uint64_t LastBaseAddr = UINT64_MAX;
Out.os() << "; Contents of section " << SectionName << "(@" << format("%08" PRIx64, SectionAddress) << "):\n";
// Dump out content as Intel-Hex.
uint64_t addr;
uint64_t end;
for (addr = 0, end = SectionContents.size(); addr < end; addr += 16) {
uint64_t LineAddr = SectionAddress + addr;
uint64_t Base = LineAddr >> 16;
uint64_t Size = (addr + 16 > end) ? end-addr : 16;
unsigned char Sum;
if (LastBaseAddr != Base) {
Sum = 6 + (Base & 0xff) + ((Base >> 8) & 0xff);
Out.os() << format(":02000004%04" PRIx64 "%02" PRIx8 "\n", Base, (unsigned char)(-Sum));
LastBaseAddr = Base;
}
// Dump line header.
Sum = Size + (LineAddr & 0xff) + ((LineAddr >> 8) & 0xff);
Out.os() << format(":%02" PRIx64 "%04" PRIx64 "00", Size, LineAddr & 0xffff);
// Dump line of hex.
for (uint64_t i = 0; i < Size; ++i) {
Sum += SectionContents[addr + i];
Out.os() << format("%02" PRIx8, (unsigned char)(SectionContents[addr + i]));
}
// Dump checksum byte.
Out.os() << format("%02" PRIx8 "\n", (unsigned char)(-Sum));
}
}
};
class ObjectCopyReadMemH : public ObjectCopyBase {
public:
ObjectCopyReadMemH(StringRef InputFilename)
: ObjectCopyBase(InputFilename) {}
virtual ~ObjectCopyReadMemH() {}
protected:
virtual void PrintSection(tool_output_file &Out, const StringRef &SectionName,
const StringRef &SectionContents, uint64_t SectionAddress) const
{
// Dump address
Out.os() << "@" << format("%" PRIx64, SectionAddress) << "\n";
uint64_t addr;
uint64_t end;
for (addr = 0, end = SectionContents.size(); addr < end; ++addr) {
// Dump hex value.
Out.os() << format("%02" PRIx8 "\n", (unsigned char)(SectionContents[addr]));
}
}
};
class ObjectCopyBinary : public ObjectCopyBase {
public:
ObjectCopyBinary(StringRef InputFilename)
: ObjectCopyBase(InputFilename)
{
mBinaryOutput = true;
mFillGaps = true;
}
virtual ~ObjectCopyBinary() {}
protected:
virtual void PrintSection(tool_output_file &Out, const StringRef &SectionName,
const StringRef &SectionContents, uint64_t SectionAddress) const
{
uint64_t addr;
uint64_t end;
for (addr = 0, end = SectionContents.size(); addr < end; ++addr) {
Out.os() << SectionContents[addr];
}
}
virtual void FillGap(tool_output_file &Out, unsigned char Value, uint64_t Size) const
{
uint64_t i;
for (i = 0; i < Size; ++i) {
Out.os() << Value;
}
}
};
int main(int argc, char **argv) {
// Print a stack trace if we signal out.
sys::PrintStackTraceOnErrorSignal();
PrettyStackTraceProgram X(argc, argv);
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
cl::ParseCommandLineOptions(argc, argv, "llvm object file copy utility\n");
ToolName = argv[0];
OwningPtr<ObjectCopyBase> ObjectCopy;
switch (OutputTarget) {
case OutputFormatTy::binary:
ObjectCopy.reset(new ObjectCopyBinary(InputFilename));
break;
case OutputFormatTy::intel_hex:
ObjectCopy.reset(new ObjectCopyIntelHex(InputFilename));
break;
case OutputFormatTy::readmemh:
ObjectCopy.reset(new ObjectCopyReadMemH(InputFilename));
break;
default:
return 1;
}
// If file isn't stdin, check that it exists.
if (InputFilename != "-" && !sys::fs::exists(InputFilename)) {
errs() << ToolName << ": '" << InputFilename << "': " << "No such file\n";
return 1;
}
// Attempt to open binary.
OwningPtr<Binary> binary;
if (error_code ec = createBinary(InputFilename, binary)) {
errs() << ToolName << ": '" << InputFilename << "': " << ec.message() << ".\n";
return 1;
}
ObjectFile *o = dyn_cast<ObjectFile>(binary.get());
if (o == NULL) {
errs() << ToolName << ": '" << InputFilename << "': " << "Unrecognized file type.\n";
}
ObjectCopy->CopyTo(o, OutputFilename);
return 0;
}