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 Archive
* archive
,
109 const std::string
& member_name
,
110 const Symbol
* sym
, const char* why
)
112 // We print a header before the list of archive members, mainly for
113 // GNU ld compatibility.
114 if (!this->printed_archive_header_
)
116 fprintf(this->map_file_
,
117 _("Archive member included because of file (symbol)\n\n"));
118 this->printed_archive_header_
= true;
121 fprintf(this->map_file_
, "%s(%s)", archive
->file().filename().c_str(),
122 member_name
.c_str());
124 size_t len
= (archive
->file().filename().length()
125 + member_name
.length()
127 this->advance_to_column(len
, 30);
130 fprintf(this->map_file_
, "%s", why
);
133 switch (sym
->source())
135 case Symbol::FROM_OBJECT
:
136 fprintf(this->map_file_
, "%s", sym
->object()->name().c_str());
139 case Symbol::IS_UNDEFINED
:
140 fprintf(this->map_file_
, "-u");
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.
151 fprintf(this->map_file_
, " (%s)", sym
->name());
154 putc('\n', this->map_file_
);
157 // Report allocating a common symbol.
160 Mapfile::report_allocate_common(const Symbol
* sym
, uint64_t symsize
)
162 if (!this->printed_common_header_
)
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;
170 std::string demangled_name
= sym
->demangled_name();
171 fprintf(this->map_file_
, "%s", demangled_name
.c_str());
173 this->advance_to_column(demangled_name
.length(), 20);
176 snprintf(buf
, sizeof buf
, "0x%llx", static_cast<unsigned long long>(symsize
));
177 fprintf(this->map_file_
, "%s", buf
);
179 size_t len
= strlen(buf
);
182 putc(' ', this->map_file_
);
186 fprintf(this->map_file_
, "%s\n", sym
->object()->name().c_str());
189 // The space we make for a section name.
191 const size_t Mapfile::section_name_map_length
= 16;
193 // Print the memory map header if necessary.
196 Mapfile::print_memory_map_header()
198 if (!this->printed_memory_map_header_
)
200 fprintf(this->map_file_
, _("\nMemory map\n\n"));
201 this->printed_memory_map_header_
= true;
205 // Print the symbols associated with an input section.
207 template<int size
, bool big_endian
>
209 Mapfile::print_input_section_symbols(
210 const Sized_relobj
<size
, big_endian
>* relobj
,
213 unsigned int symcount
= relobj
->symbol_count();
214 for (unsigned int i
= relobj
->local_symbol_count(); i
< symcount
; ++i
)
216 const Symbol
* sym
= relobj
->global_symbol(i
);
219 && sym
->source() == Symbol::FROM_OBJECT
220 && sym
->object() == relobj
221 && sym
->shndx(&is_ordinary
) == shndx
223 && sym
->is_defined())
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_
,
232 static_cast<unsigned long long>(ssym
->value()),
233 sym
->demangled_name().c_str());
238 // Print an input section.
241 Mapfile::print_input_section(Relobj
* relobj
, unsigned int shndx
)
243 putc(' ', this->map_file_
);
245 std::string name
= relobj
->section_name(shndx
);
246 fprintf(this->map_file_
, "%s", name
.c_str());
248 this->advance_to_column(name
.length() + 1, Mapfile::section_name_map_length
);
252 if (!relobj
->is_section_included(shndx
))
259 section_offset_type offset
;
260 os
= relobj
->output_section(shndx
, &offset
);
264 addr
= os
->address() + offset
;
268 snprintf(sizebuf
, sizeof sizebuf
, "0x%llx",
269 static_cast<unsigned long long>(relobj
->section_size(shndx
)));
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());
278 switch (parameters
->size_and_endianness())
280 #ifdef HAVE_TARGET_32_LITTLE
281 case Parameters::TARGET_32_LITTLE
:
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
);
289 #ifdef HAVE_TARGET_32_BIG
290 case Parameters::TARGET_32_BIG
:
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
);
298 #ifdef HAVE_TARGET_64_LITTLE
299 case Parameters::TARGET_64_LITTLE
:
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
);
307 #ifdef HAVE_TARGET_64_BIG
308 case Parameters::TARGET_64_BIG
:
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
);
322 // Print an Output_section_data. This is printed to look like an
326 Mapfile::print_output_data(const Output_data
* od
, const char* name
)
328 this->print_memory_map_header();
330 putc(' ', this->map_file_
);
332 fprintf(this->map_file_
, "%s", name
);
334 this->advance_to_column(strlen(name
) + 1, Mapfile::section_name_map_length
);
337 snprintf(sizebuf
, sizeof sizebuf
, "0x%llx",
338 static_cast<unsigned long long>(od
->data_size()));
340 fprintf(this->map_file_
, "0x%0*llx %10s\n",
341 parameters
->target().get_size() / 4,
342 static_cast<unsigned long long>(od
->address()),
346 // Print the discarded input sections.
349 Mapfile::print_discarded_sections(const Input_objects
* input_objects
)
351 bool printed_header
= false;
352 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
353 p
!= input_objects
->relobj_end();
357 unsigned int shnum
= relobj
->shnum();
358 for (unsigned int i
= 0; i
< shnum
; ++i
)
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
))
368 fprintf(this->map_file_
, _("\nDiscarded input sections\n\n"));
369 printed_header
= true;
372 this->print_input_section(relobj
, i
);
378 // Print an output section.
381 Mapfile::print_output_section(const Output_section
* os
)
383 this->print_memory_map_header();
385 fprintf(this->map_file_
, "\n%s", os
->name());
387 this->advance_to_column(strlen(os
->name()), Mapfile::section_name_map_length
);
390 snprintf(sizebuf
, sizeof sizebuf
, "0x%llx",
391 static_cast<unsigned long long>(os
->data_size()));
393 fprintf(this->map_file_
, "0x%0*llx %10s",
394 parameters
->target().get_size() / 4,
395 static_cast<unsigned long long>(os
->address()), sizebuf
);
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()));
402 putc('\n', this->map_file_
);
405 } // End namespace gold.