Handle output sections with more than 0x7fffffff bytes.
[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 os = relobj->output_section(shndx);
260 addr = relobj->output_section_offset(shndx);
261 if (addr != -1U)
262 addr += os->address();
263 }
264
265 char sizebuf[50];
266 snprintf(sizebuf, sizeof sizebuf, "0x%llx",
267 static_cast<unsigned long long>(relobj->section_size(shndx)));
268
269 fprintf(this->map_file_, "0x%0*llx %10s %s\n",
270 parameters->target().get_size() / 4,
271 static_cast<unsigned long long>(addr), sizebuf,
272 relobj->name().c_str());
273
274 if (os != NULL)
275 {
276 switch (parameters->size_and_endianness())
277 {
278 #ifdef HAVE_TARGET_32_LITTLE
279 case Parameters::TARGET_32_LITTLE:
280 {
281 const Sized_relobj<32, false>* sized_relobj =
282 static_cast<Sized_relobj<32, false>*>(relobj);
283 this->print_input_section_symbols(sized_relobj, shndx);
284 }
285 break;
286 #endif
287 #ifdef HAVE_TARGET_32_BIG
288 case Parameters::TARGET_32_BIG:
289 {
290 const Sized_relobj<32, true>* sized_relobj =
291 static_cast<Sized_relobj<32, true>*>(relobj);
292 this->print_input_section_symbols(sized_relobj, shndx);
293 }
294 break;
295 #endif
296 #ifdef HAVE_TARGET_64_LITTLE
297 case Parameters::TARGET_64_LITTLE:
298 {
299 const Sized_relobj<64, false>* sized_relobj =
300 static_cast<Sized_relobj<64, false>*>(relobj);
301 this->print_input_section_symbols(sized_relobj, shndx);
302 }
303 break;
304 #endif
305 #ifdef HAVE_TARGET_64_BIG
306 case Parameters::TARGET_64_BIG:
307 {
308 const Sized_relobj<64, true>* sized_relobj =
309 static_cast<Sized_relobj<64, true>*>(relobj);
310 this->print_input_section_symbols(sized_relobj, shndx);
311 }
312 break;
313 #endif
314 default:
315 gold_unreachable();
316 }
317 }
318 }
319
320 // Print an Output_section_data. This is printed to look like an
321 // input section.
322
323 void
324 Mapfile::print_output_data(const Output_data* od, const char* name)
325 {
326 this->print_memory_map_header();
327
328 putc(' ', this->map_file_);
329
330 fprintf(this->map_file_, "%s", name);
331
332 this->advance_to_column(strlen(name) + 1, Mapfile::section_name_map_length);
333
334 char sizebuf[50];
335 snprintf(sizebuf, sizeof sizebuf, "0x%llx",
336 static_cast<unsigned long long>(od->data_size()));
337
338 fprintf(this->map_file_, "0x%0*llx %10s\n",
339 parameters->target().get_size() / 4,
340 static_cast<unsigned long long>(od->address()),
341 sizebuf);
342 }
343
344 // Print the discarded input sections.
345
346 void
347 Mapfile::print_discarded_sections(const Input_objects* input_objects)
348 {
349 bool printed_header = false;
350 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
351 p != input_objects->relobj_end();
352 ++p)
353 {
354 Relobj* relobj = *p;
355 unsigned int shnum = relobj->shnum();
356 for (unsigned int i = 0; i < shnum; ++i)
357 {
358 unsigned int sh_type = relobj->section_type(i);
359 if ((sh_type == elfcpp::SHT_PROGBITS
360 || sh_type == elfcpp::SHT_NOBITS
361 || sh_type == elfcpp::SHT_GROUP)
362 && !relobj->is_section_included(i))
363 {
364 if (!printed_header)
365 {
366 fprintf(this->map_file_, _("\nDiscarded input sections\n\n"));
367 printed_header = true;
368 }
369
370 this->print_input_section(relobj, i);
371 }
372 }
373 }
374 }
375
376 // Print an output section.
377
378 void
379 Mapfile::print_output_section(const Output_section* os)
380 {
381 this->print_memory_map_header();
382
383 fprintf(this->map_file_, "\n%s", os->name());
384
385 this->advance_to_column(strlen(os->name()), Mapfile::section_name_map_length);
386
387 char sizebuf[50];
388 snprintf(sizebuf, sizeof sizebuf, "0x%llx",
389 static_cast<unsigned long long>(os->data_size()));
390
391 fprintf(this->map_file_, "0x%0*llx %10s",
392 parameters->target().get_size() / 4,
393 static_cast<unsigned long long>(os->address()), sizebuf);
394
395 if (os->has_load_address())
396 fprintf(this->map_file_, " load address 0x%-*llx",
397 parameters->target().get_size() / 4,
398 static_cast<unsigned long long>(os->load_address()));
399
400 putc('\n', this->map_file_);
401 }
402
403 } // End namespace gold.
This page took 0.040711 seconds and 5 git commands to generate.