* mapfile.cc: New file.
[deliverable/binutils-gdb.git] / gold / mapfile.cc
1 // mapfile.cc -- map file generation for gold
2
3 // Copyright 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cerrno>
26 #include <cstdio>
27 #include <cstring>
28
29 #include "archive.h"
30 #include "symtab.h"
31 #include "output.h"
32 #include "mapfile.h"
33
34 // This file holds the code for printing information to the map file.
35 // In general we try to produce pretty much the same format as GNU ld.
36
37 namespace gold
38 {
39
40 // Mapfile constructor.
41
42 Mapfile::Mapfile()
43 : map_file_(NULL),
44 printed_archive_header_(false),
45 printed_common_header_(false),
46 printed_memory_map_header_(false)
47 {
48 }
49
50 // Mapfile destructor.
51
52 Mapfile::~Mapfile()
53 {
54 if (this->map_file_ != NULL)
55 this->close();
56 }
57
58 // Open the map file.
59
60 bool
61 Mapfile::open(const char* map_filename)
62 {
63 if (strcmp(map_filename, "-") == 0)
64 this->map_file_ = stdout;
65 else
66 {
67 this->map_file_ = ::fopen(map_filename, "w");
68 if (this->map_file_ == NULL)
69 {
70 gold_error(_("cannot open map file %s: %s"), map_filename,
71 strerror(errno));
72 return false;
73 }
74 }
75 return true;
76 }
77
78 // Close the map file.
79
80 void
81 Mapfile::close()
82 {
83 if (fclose(this->map_file_) != 0)
84 gold_error(_("cannot close map file: %s"), strerror(errno));
85 this->map_file_ = NULL;
86 }
87
88 // Advance to a column.
89
90 void
91 Mapfile::advance_to_column(size_t from, size_t to)
92 {
93 if (from >= to - 1)
94 {
95 putc('\n', this->map_file_);
96 from = 0;
97 }
98 while (from < to)
99 {
100 putc(' ', this->map_file_);
101 ++from;
102 }
103 }
104
105 // Report about including a member from an archive.
106
107 void
108 Mapfile::report_include_archive_member(const Archive* archive,
109 const std::string& member_name,
110 const Symbol* sym, const char* why)
111 {
112 // We print a header before the list of archive members, mainly for
113 // GNU ld compatibility.
114 if (!this->printed_archive_header_)
115 {
116 fprintf(this->map_file_,
117 _("Archive member included because of file (symbol)\n\n"));
118 this->printed_archive_header_ = true;
119 }
120
121 fprintf(this->map_file_, "%s(%s)", archive->file().filename().c_str(),
122 member_name.c_str());
123
124 size_t len = (archive->file().filename().length()
125 + member_name.length()
126 + 2);
127 this->advance_to_column(len, 30);
128
129 if (sym == NULL)
130 fprintf(this->map_file_, "%s", why);
131 else
132 {
133 switch (sym->source())
134 {
135 case Symbol::FROM_OBJECT:
136 fprintf(this->map_file_, "%s", sym->object()->name().c_str());
137 break;
138
139 case Symbol::IS_UNDEFINED:
140 fprintf(this->map_file_, "-u");
141 break;
142
143 default:
144 case Symbol::IN_OUTPUT_DATA:
145 case Symbol::IN_OUTPUT_SEGMENT:
146 case Symbol::IS_CONSTANT:
147 // We should only see an undefined symbol here.
148 gold_unreachable();
149 }
150
151 fprintf(this->map_file_, " (%s)", sym->name());
152 }
153
154 putc('\n', this->map_file_);
155 }
156
157 // Report allocating a common symbol.
158
159 void
160 Mapfile::report_allocate_common(const Symbol* sym, uint64_t symsize)
161 {
162 if (!this->printed_common_header_)
163 {
164 fprintf(this->map_file_, _("\nAllocating common symbols\n"));
165 fprintf(this->map_file_,
166 _("Common symbol size file\n\n"));
167 this->printed_common_header_ = true;
168 }
169
170 std::string demangled_name = sym->demangled_name();
171 fprintf(this->map_file_, "%s", demangled_name.c_str());
172
173 this->advance_to_column(demangled_name.length(), 20);
174
175 char buf[50];
176 snprintf(buf, sizeof buf, "0x%llx", static_cast<unsigned long long>(symsize));
177 fprintf(this->map_file_, "%s", buf);
178
179 size_t len = strlen(buf);
180 while (len < 18)
181 {
182 putc(' ', this->map_file_);
183 ++len;
184 }
185
186 fprintf(this->map_file_, "%s\n", sym->object()->name().c_str());
187 }
188
189 // The space we make for a section name.
190
191 const size_t Mapfile::section_name_map_length = 16;
192
193 // Print the memory map header if necessary.
194
195 void
196 Mapfile::print_memory_map_header()
197 {
198 if (!this->printed_memory_map_header_)
199 {
200 fprintf(this->map_file_, _("\nMemory map\n\n"));
201 this->printed_memory_map_header_ = true;
202 }
203 }
204
205 // Print the symbols associated with an input section.
206
207 template<int size, bool big_endian>
208 void
209 Mapfile::print_input_section_symbols(
210 const Sized_relobj<size, big_endian>* relobj,
211 unsigned int shndx)
212 {
213 unsigned int symcount = relobj->symbol_count();
214 for (unsigned int i = relobj->local_symbol_count(); i < symcount; ++i)
215 {
216 const Symbol* sym = relobj->global_symbol(i);
217 bool is_ordinary;
218 if (sym != NULL
219 && sym->source() == Symbol::FROM_OBJECT
220 && sym->object() == relobj
221 && sym->shndx(&is_ordinary) == shndx
222 && is_ordinary
223 && sym->is_defined())
224 {
225 for (size_t i = 0; i < Mapfile::section_name_map_length; ++i)
226 putc(' ', this->map_file_);
227 const Sized_symbol<size>* ssym =
228 static_cast<const Sized_symbol<size>*>(sym);
229 fprintf(this->map_file_,
230 "0x%0*llx %s\n",
231 size / 4,
232 static_cast<unsigned long long>(ssym->value()),
233 sym->demangled_name().c_str());
234 }
235 }
236 }
237
238 // Print an input section.
239
240 void
241 Mapfile::print_input_section(Relobj* relobj, unsigned int shndx)
242 {
243 putc(' ', this->map_file_);
244
245 std::string name = relobj->section_name(shndx);
246 fprintf(this->map_file_, "%s", name.c_str());
247
248 this->advance_to_column(name.length() + 1, Mapfile::section_name_map_length);
249
250 Output_section* os;
251 uint64_t addr;
252 if (!relobj->is_section_included(shndx))
253 {
254 os = NULL;
255 addr = 0;
256 }
257 else
258 {
259 section_offset_type offset;
260 os = relobj->output_section(shndx, &offset);
261 if (offset == -1)
262 addr = ~0ULL;
263 else
264 addr = os->address() + offset;
265 }
266
267 char sizebuf[50];
268 snprintf(sizebuf, sizeof sizebuf, "0x%llx",
269 static_cast<unsigned long long>(relobj->section_size(shndx)));
270
271 fprintf(this->map_file_, "0x%0*llx %10s %s\n",
272 parameters->target().get_size() / 4,
273 static_cast<unsigned long long>(addr), sizebuf,
274 relobj->name().c_str());
275
276 if (os != NULL)
277 {
278 switch (parameters->size_and_endianness())
279 {
280 #ifdef HAVE_TARGET_32_LITTLE
281 case Parameters::TARGET_32_LITTLE:
282 {
283 const Sized_relobj<32, false>* sized_relobj =
284 static_cast<Sized_relobj<32, false>*>(relobj);
285 this->print_input_section_symbols(sized_relobj, shndx);
286 }
287 break;
288 #endif
289 #ifdef HAVE_TARGET_32_BIG
290 case Parameters::TARGET_32_BIG:
291 {
292 const Sized_relobj<32, true>* sized_relobj =
293 static_cast<Sized_relobj<32, true>*>(relobj);
294 this->print_input_section_symbols(sized_relobj, shndx);
295 }
296 break;
297 #endif
298 #ifdef HAVE_TARGET_64_LITTLE
299 case Parameters::TARGET_64_LITTLE:
300 {
301 const Sized_relobj<64, false>* sized_relobj =
302 static_cast<Sized_relobj<64, false>*>(relobj);
303 this->print_input_section_symbols(sized_relobj, shndx);
304 }
305 break;
306 #endif
307 #ifdef HAVE_TARGET_64_BIG
308 case Parameters::TARGET_64_BIG:
309 {
310 const Sized_relobj<64, true>* sized_relobj =
311 static_cast<Sized_relobj<64, true>*>(relobj);
312 this->print_input_section_symbols(sized_relobj, shndx);
313 }
314 break;
315 #endif
316 default:
317 gold_unreachable();
318 }
319 }
320 }
321
322 // Print an Output_section_data. This is printed to look like an
323 // input section.
324
325 void
326 Mapfile::print_output_data(const Output_data* od, const char* name)
327 {
328 this->print_memory_map_header();
329
330 putc(' ', this->map_file_);
331
332 fprintf(this->map_file_, "%s", name);
333
334 this->advance_to_column(strlen(name) + 1, Mapfile::section_name_map_length);
335
336 char sizebuf[50];
337 snprintf(sizebuf, sizeof sizebuf, "0x%llx",
338 static_cast<unsigned long long>(od->data_size()));
339
340 fprintf(this->map_file_, "0x%0*llx %10s\n",
341 parameters->target().get_size() / 4,
342 static_cast<unsigned long long>(od->address()),
343 sizebuf);
344 }
345
346 // Print the discarded input sections.
347
348 void
349 Mapfile::print_discarded_sections(const Input_objects* input_objects)
350 {
351 bool printed_header = false;
352 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
353 p != input_objects->relobj_end();
354 ++p)
355 {
356 Relobj* relobj = *p;
357 unsigned int shnum = relobj->shnum();
358 for (unsigned int i = 0; i < shnum; ++i)
359 {
360 unsigned int sh_type = relobj->section_type(i);
361 if ((sh_type == elfcpp::SHT_PROGBITS
362 || sh_type == elfcpp::SHT_NOBITS
363 || sh_type == elfcpp::SHT_GROUP)
364 && !relobj->is_section_included(i))
365 {
366 if (!printed_header)
367 {
368 fprintf(this->map_file_, _("\nDiscarded input sections\n\n"));
369 printed_header = true;
370 }
371
372 this->print_input_section(relobj, i);
373 }
374 }
375 }
376 }
377
378 // Print an output section.
379
380 void
381 Mapfile::print_output_section(const Output_section* os)
382 {
383 this->print_memory_map_header();
384
385 fprintf(this->map_file_, "\n%s", os->name());
386
387 this->advance_to_column(strlen(os->name()), Mapfile::section_name_map_length);
388
389 char sizebuf[50];
390 snprintf(sizebuf, sizeof sizebuf, "0x%llx",
391 static_cast<unsigned long long>(os->data_size()));
392
393 fprintf(this->map_file_, "0x%0*llx %10s",
394 parameters->target().get_size() / 4,
395 static_cast<unsigned long long>(os->address()), sizebuf);
396
397 if (os->has_load_address())
398 fprintf(this->map_file_, " load address 0x%-*llx",
399 parameters->target().get_size() / 4,
400 static_cast<unsigned long long>(os->load_address()));
401
402 putc('\n', this->map_file_);
403 }
404
405 } // End namespace gold.
This page took 0.038259 seconds and 5 git commands to generate.