1 // mapfile.cc -- map file generation for gold
3 // Copyright 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
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.
40 // Mapfile constructor.
44 printed_archive_header_(false),
45 printed_common_header_(false),
46 printed_memory_map_header_(false)
50 // Mapfile destructor.
54 if (this->map_file_
!= NULL
)
61 Mapfile::open(const char* map_filename
)
63 if (strcmp(map_filename
, "-") == 0)
64 this->map_file_
= stdout
;
67 this->map_file_
= ::fopen(map_filename
, "w");
68 if (this->map_file_
== NULL
)
70 gold_error(_("cannot open map file %s: %s"), map_filename
,
78 // Close the map file.
83 if (fclose(this->map_file_
) != 0)
84 gold_error(_("cannot close map file: %s"), strerror(errno
));
85 this->map_file_
= NULL
;
88 // Advance to a column.
91 Mapfile::advance_to_column(size_t from
, size_t to
)
95 putc('\n', this->map_file_
);
100 putc(' ', this->map_file_
);
105 // Report about including a member from an archive.
108 Mapfile::report_include_archive_member(const std::string
& member_name
,
109 const Symbol
* sym
, const char* why
)
111 // We print a header before the list of archive members, mainly for
112 // GNU ld compatibility.
113 if (!this->printed_archive_header_
)
115 fprintf(this->map_file_
,
116 _("Archive member included because of file (symbol)\n\n"));
117 this->printed_archive_header_
= true;
120 fprintf(this->map_file_
, "%s", member_name
.c_str());
122 this->advance_to_column(member_name
.length(), 30);
125 fprintf(this->map_file_
, "%s", why
);
128 switch (sym
->source())
130 case Symbol::FROM_OBJECT
:
131 fprintf(this->map_file_
, "%s", sym
->object()->name().c_str());
134 case Symbol::IS_UNDEFINED
:
135 fprintf(this->map_file_
, "-u");
139 case Symbol::IN_OUTPUT_DATA
:
140 case Symbol::IN_OUTPUT_SEGMENT
:
141 case Symbol::IS_CONSTANT
:
142 // We should only see an undefined symbol here.
146 fprintf(this->map_file_
, " (%s)", sym
->name());
149 putc('\n', this->map_file_
);
152 // Report allocating a common symbol.
155 Mapfile::report_allocate_common(const Symbol
* sym
, uint64_t symsize
)
157 if (!this->printed_common_header_
)
159 fprintf(this->map_file_
, _("\nAllocating common symbols\n"));
160 fprintf(this->map_file_
,
161 _("Common symbol size file\n\n"));
162 this->printed_common_header_
= true;
165 std::string demangled_name
= sym
->demangled_name();
166 fprintf(this->map_file_
, "%s", demangled_name
.c_str());
168 this->advance_to_column(demangled_name
.length(), 20);
171 snprintf(buf
, sizeof buf
, "0x%llx", static_cast<unsigned long long>(symsize
));
172 fprintf(this->map_file_
, "%s", buf
);
174 size_t len
= strlen(buf
);
177 putc(' ', this->map_file_
);
181 fprintf(this->map_file_
, "%s\n", sym
->object()->name().c_str());
184 // The space we make for a section name.
186 const size_t Mapfile::section_name_map_length
= 16;
188 // Print the memory map header if necessary.
191 Mapfile::print_memory_map_header()
193 if (!this->printed_memory_map_header_
)
195 fprintf(this->map_file_
, _("\nMemory map\n\n"));
196 this->printed_memory_map_header_
= true;
200 // Print the symbols associated with an input section.
202 template<int size
, bool big_endian
>
204 Mapfile::print_input_section_symbols(
205 const Sized_relobj_file
<size
, big_endian
>* relobj
,
208 unsigned int symcount
= relobj
->symbol_count();
209 for (unsigned int i
= relobj
->local_symbol_count(); i
< symcount
; ++i
)
211 const Symbol
* sym
= relobj
->global_symbol(i
);
214 && sym
->source() == Symbol::FROM_OBJECT
215 && sym
->object() == relobj
216 && sym
->shndx(&is_ordinary
) == shndx
218 && sym
->is_defined())
220 for (size_t i
= 0; i
< Mapfile::section_name_map_length
; ++i
)
221 putc(' ', this->map_file_
);
222 const Sized_symbol
<size
>* ssym
=
223 static_cast<const Sized_symbol
<size
>*>(sym
);
224 fprintf(this->map_file_
,
227 static_cast<unsigned long long>(ssym
->value()),
228 sym
->demangled_name().c_str());
233 // Print an input section.
236 Mapfile::print_input_section(Relobj
* relobj
, unsigned int shndx
)
238 putc(' ', this->map_file_
);
240 std::string name
= relobj
->section_name(shndx
);
241 fprintf(this->map_file_
, "%s", name
.c_str());
243 this->advance_to_column(name
.length() + 1, Mapfile::section_name_map_length
);
247 if (!relobj
->is_section_included(shndx
))
254 os
= relobj
->output_section(shndx
);
255 addr
= relobj
->output_section_offset(shndx
);
257 addr
+= os
->address();
261 snprintf(sizebuf
, sizeof sizebuf
, "0x%llx",
262 static_cast<unsigned long long>(relobj
->section_size(shndx
)));
264 fprintf(this->map_file_
, "0x%0*llx %10s %s\n",
265 parameters
->target().get_size() / 4,
266 static_cast<unsigned long long>(addr
), sizebuf
,
267 relobj
->name().c_str());
271 switch (parameters
->size_and_endianness())
273 #ifdef HAVE_TARGET_32_LITTLE
274 case Parameters::TARGET_32_LITTLE
:
276 const Sized_relobj_file
<32, false>* sized_relobj
=
277 static_cast<Sized_relobj_file
<32, false>*>(relobj
);
278 this->print_input_section_symbols(sized_relobj
, shndx
);
282 #ifdef HAVE_TARGET_32_BIG
283 case Parameters::TARGET_32_BIG
:
285 const Sized_relobj_file
<32, true>* sized_relobj
=
286 static_cast<Sized_relobj_file
<32, true>*>(relobj
);
287 this->print_input_section_symbols(sized_relobj
, shndx
);
291 #ifdef HAVE_TARGET_64_LITTLE
292 case Parameters::TARGET_64_LITTLE
:
294 const Sized_relobj_file
<64, false>* sized_relobj
=
295 static_cast<Sized_relobj_file
<64, false>*>(relobj
);
296 this->print_input_section_symbols(sized_relobj
, shndx
);
300 #ifdef HAVE_TARGET_64_BIG
301 case Parameters::TARGET_64_BIG
:
303 const Sized_relobj_file
<64, true>* sized_relobj
=
304 static_cast<Sized_relobj_file
<64, true>*>(relobj
);
305 this->print_input_section_symbols(sized_relobj
, shndx
);
315 // Print an Output_section_data. This is printed to look like an
319 Mapfile::print_output_data(const Output_data
* od
, const char* name
)
321 this->print_memory_map_header();
323 putc(' ', this->map_file_
);
325 fprintf(this->map_file_
, "%s", name
);
327 this->advance_to_column(strlen(name
) + 1, Mapfile::section_name_map_length
);
330 snprintf(sizebuf
, sizeof sizebuf
, "0x%llx",
331 static_cast<unsigned long long>(od
->data_size()));
333 fprintf(this->map_file_
, "0x%0*llx %10s\n",
334 parameters
->target().get_size() / 4,
335 static_cast<unsigned long long>(od
->address()),
339 // Print the discarded input sections.
342 Mapfile::print_discarded_sections(const Input_objects
* input_objects
)
344 bool printed_header
= false;
345 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
346 p
!= input_objects
->relobj_end();
350 // Lock the object so we can read from it. This is only called
351 // single-threaded from Layout_task_runner, so it is OK to lock.
352 // Unfortunately we have no way to pass in a Task token.
353 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
354 Task_lock_obj
<Object
> tl(dummy_task
, relobj
);
356 unsigned int shnum
= relobj
->shnum();
357 for (unsigned int i
= 0; i
< shnum
; ++i
)
359 unsigned int sh_type
= relobj
->section_type(i
);
360 if ((sh_type
== elfcpp::SHT_PROGBITS
361 || sh_type
== elfcpp::SHT_NOBITS
362 || sh_type
== elfcpp::SHT_GROUP
)
363 && !relobj
->is_section_included(i
))
367 fprintf(this->map_file_
, _("\nDiscarded input sections\n\n"));
368 printed_header
= true;
371 this->print_input_section(relobj
, i
);
377 // Print an output section.
380 Mapfile::print_output_section(const Output_section
* os
)
382 this->print_memory_map_header();
384 fprintf(this->map_file_
, "\n%s", os
->name());
386 this->advance_to_column(strlen(os
->name()), Mapfile::section_name_map_length
);
389 snprintf(sizebuf
, sizeof sizebuf
, "0x%llx",
390 static_cast<unsigned long long>(os
->data_size()));
392 fprintf(this->map_file_
, "0x%0*llx %10s",
393 parameters
->target().get_size() / 4,
394 static_cast<unsigned long long>(os
->address()), sizebuf
);
396 if (os
->has_load_address())
397 fprintf(this->map_file_
, " load address 0x%-*llx",
398 parameters
->target().get_size() / 4,
399 static_cast<unsigned long long>(os
->load_address()));
401 putc('\n', this->map_file_
);
404 } // End namespace gold.