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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
// mapfile.cc -- map file generation for gold
// Copyright 2008 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <iant@google.com>.
// This file is part of gold.
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
// MA 02110-1301, USA.
#include "gold.h"
#include <cerrno>
#include <cstdio>
#include <cstring>
#include "archive.h"
#include "symtab.h"
#include "output.h"
#include "mapfile.h"
// This file holds the code for printing information to the map file.
// In general we try to produce pretty much the same format as GNU ld.
namespace gold
{
// Mapfile constructor.
Mapfile::Mapfile()
: map_file_(NULL),
printed_archive_header_(false),
printed_common_header_(false),
printed_memory_map_header_(false)
{
}
// Mapfile destructor.
Mapfile::~Mapfile()
{
if (this->map_file_ != NULL)
this->close();
}
// Open the map file.
bool
Mapfile::open(const char* map_filename)
{
if (strcmp(map_filename, "-") == 0)
this->map_file_ = stdout;
else
{
this->map_file_ = ::fopen(map_filename, "w");
if (this->map_file_ == NULL)
{
gold_error(_("cannot open map file %s: %s"), map_filename,
strerror(errno));
return false;
}
}
return true;
}
// Close the map file.
void
Mapfile::close()
{
if (fclose(this->map_file_) != 0)
gold_error(_("cannot close map file: %s"), strerror(errno));
this->map_file_ = NULL;
}
// Advance to a column.
void
Mapfile::advance_to_column(size_t from, size_t to)
{
if (from >= to - 1)
{
putc('\n', this->map_file_);
from = 0;
}
while (from < to)
{
putc(' ', this->map_file_);
++from;
}
}
// Report about including a member from an archive.
void
Mapfile::report_include_archive_member(const std::string& member_name,
const Symbol* sym, const char* why)
{
// We print a header before the list of archive members, mainly for
// GNU ld compatibility.
if (!this->printed_archive_header_)
{
fprintf(this->map_file_,
_("Archive member included because of file (symbol)\n\n"));
this->printed_archive_header_ = true;
}
fprintf(this->map_file_, "%s", member_name.c_str());
this->advance_to_column(member_name.length(), 30);
if (sym == NULL)
fprintf(this->map_file_, "%s", why);
else
{
switch (sym->source())
{
case Symbol::FROM_OBJECT:
fprintf(this->map_file_, "%s", sym->object()->name().c_str());
break;
case Symbol::IS_UNDEFINED:
fprintf(this->map_file_, "-u");
break;
default:
case Symbol::IN_OUTPUT_DATA:
case Symbol::IN_OUTPUT_SEGMENT:
case Symbol::IS_CONSTANT:
// We should only see an undefined symbol here.
gold_unreachable();
}
fprintf(this->map_file_, " (%s)", sym->name());
}
putc('\n', this->map_file_);
}
// Report allocating a common symbol.
void
Mapfile::report_allocate_common(const Symbol* sym, uint64_t symsize)
{
if (!this->printed_common_header_)
{
fprintf(this->map_file_, _("\nAllocating common symbols\n"));
fprintf(this->map_file_,
_("Common symbol size file\n\n"));
this->printed_common_header_ = true;
}
std::string demangled_name = sym->demangled_name();
fprintf(this->map_file_, "%s", demangled_name.c_str());
this->advance_to_column(demangled_name.length(), 20);
char buf[50];
snprintf(buf, sizeof buf, "0x%llx", static_cast<unsigned long long>(symsize));
fprintf(this->map_file_, "%s", buf);
size_t len = strlen(buf);
while (len < 18)
{
putc(' ', this->map_file_);
++len;
}
fprintf(this->map_file_, "%s\n", sym->object()->name().c_str());
}
// The space we make for a section name.
const size_t Mapfile::section_name_map_length = 16;
// Print the memory map header if necessary.
void
Mapfile::print_memory_map_header()
{
if (!this->printed_memory_map_header_)
{
fprintf(this->map_file_, _("\nMemory map\n\n"));
this->printed_memory_map_header_ = true;
}
}
// Print the symbols associated with an input section.
template<int size, bool big_endian>
void
Mapfile::print_input_section_symbols(
const Sized_relobj<size, big_endian>* relobj,
unsigned int shndx)
{
unsigned int symcount = relobj->symbol_count();
for (unsigned int i = relobj->local_symbol_count(); i < symcount; ++i)
{
const Symbol* sym = relobj->global_symbol(i);
bool is_ordinary;
if (sym != NULL
&& sym->source() == Symbol::FROM_OBJECT
&& sym->object() == relobj
&& sym->shndx(&is_ordinary) == shndx
&& is_ordinary
&& sym->is_defined())
{
for (size_t j = 0; j < Mapfile::section_name_map_length; ++j)
putc(' ', this->map_file_);
const Sized_symbol<size>* ssym =
static_cast<const Sized_symbol<size>*>(sym);
fprintf(this->map_file_,
"0x%0*llx %s\n",
size / 4,
static_cast<unsigned long long>(ssym->value()),
sym->demangled_name().c_str());
}
}
}
// Print an input section.
void
Mapfile::print_input_section(Relobj* relobj, unsigned int shndx)
{
putc(' ', this->map_file_);
std::string name = relobj->section_name(shndx);
fprintf(this->map_file_, "%s", name.c_str());
this->advance_to_column(name.length() + 1, Mapfile::section_name_map_length);
Output_section* os;
uint64_t addr;
if (!relobj->is_section_included(shndx))
{
os = NULL;
addr = 0;
}
else
{
os = relobj->output_section(shndx);
addr = relobj->output_section_offset(shndx);
if (addr != -1ULL)
addr += os->address();
}
char sizebuf[50];
snprintf(sizebuf, sizeof sizebuf, "0x%llx",
static_cast<unsigned long long>(relobj->section_size(shndx)));
fprintf(this->map_file_, "0x%0*llx %10s %s\n",
parameters->target().get_size() / 4,
static_cast<unsigned long long>(addr), sizebuf,
relobj->name().c_str());
if (os != NULL)
{
switch (parameters->size_and_endianness())
{
#ifdef HAVE_TARGET_32_LITTLE
case Parameters::TARGET_32_LITTLE:
{
const Sized_relobj<32, false>* sized_relobj =
static_cast<Sized_relobj<32, false>*>(relobj);
this->print_input_section_symbols(sized_relobj, shndx);
}
break;
#endif
#ifdef HAVE_TARGET_32_BIG
case Parameters::TARGET_32_BIG:
{
const Sized_relobj<32, true>* sized_relobj =
static_cast<Sized_relobj<32, true>*>(relobj);
this->print_input_section_symbols(sized_relobj, shndx);
}
break;
#endif
#ifdef HAVE_TARGET_64_LITTLE
case Parameters::TARGET_64_LITTLE:
{
const Sized_relobj<64, false>* sized_relobj =
static_cast<Sized_relobj<64, false>*>(relobj);
this->print_input_section_symbols(sized_relobj, shndx);
}
break;
#endif
#ifdef HAVE_TARGET_64_BIG
case Parameters::TARGET_64_BIG:
{
const Sized_relobj<64, true>* sized_relobj =
static_cast<Sized_relobj<64, true>*>(relobj);
this->print_input_section_symbols(sized_relobj, shndx);
}
break;
#endif
default:
gold_unreachable();
}
}
}
// Print an Output_section_data. This is printed to look like an
// input section.
void
Mapfile::print_output_data(const Output_data* od, const char* name)
{
this->print_memory_map_header();
putc(' ', this->map_file_);
fprintf(this->map_file_, "%s", name);
this->advance_to_column(strlen(name) + 1, Mapfile::section_name_map_length);
char sizebuf[50];
snprintf(sizebuf, sizeof sizebuf, "0x%llx",
static_cast<unsigned long long>(od->data_size()));
fprintf(this->map_file_, "0x%0*llx %10s\n",
parameters->target().get_size() / 4,
static_cast<unsigned long long>(od->address()),
sizebuf);
}
// Print the discarded input sections.
void
Mapfile::print_discarded_sections(const Input_objects* input_objects)
{
bool printed_header = false;
for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
p != input_objects->relobj_end();
++p)
{
Relobj* relobj = *p;
unsigned int shnum = relobj->shnum();
for (unsigned int i = 0; i < shnum; ++i)
{
unsigned int sh_type = relobj->section_type(i);
if ((sh_type == elfcpp::SHT_PROGBITS
|| sh_type == elfcpp::SHT_NOBITS
|| sh_type == elfcpp::SHT_GROUP)
&& !relobj->is_section_included(i))
{
if (!printed_header)
{
fprintf(this->map_file_, _("\nDiscarded input sections\n\n"));
printed_header = true;
}
this->print_input_section(relobj, i);
}
}
}
}
// Print an output section.
void
Mapfile::print_output_section(const Output_section* os)
{
this->print_memory_map_header();
fprintf(this->map_file_, "\n%s", os->name());
this->advance_to_column(strlen(os->name()), Mapfile::section_name_map_length);
char sizebuf[50];
snprintf(sizebuf, sizeof sizebuf, "0x%llx",
static_cast<unsigned long long>(os->data_size()));
fprintf(this->map_file_, "0x%0*llx %10s",
parameters->target().get_size() / 4,
static_cast<unsigned long long>(os->address()), sizebuf);
if (os->has_load_address())
fprintf(this->map_file_, " load address 0x%-*llx",
parameters->target().get_size() / 4,
static_cast<unsigned long long>(os->load_address()));
putc('\n', this->map_file_);
}
} // End namespace gold.
|