* common.h: Change registry@caldera.com to registry@sco.com.
[binutils.git] / gold / mapfile.cc
blobe053419c092698e283b710d25c15fbf7ae30ef1c
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.
23 #include "gold.h"
25 #include <cerrno>
26 #include <cstdio>
27 #include <cstring>
29 #include "archive.h"
30 #include "symtab.h"
31 #include "output.h"
32 #include "mapfile.h"
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.
37 namespace gold
40 // Mapfile constructor.
42 Mapfile::Mapfile()
43 : map_file_(NULL),
44 printed_archive_header_(false),
45 printed_common_header_(false),
46 printed_memory_map_header_(false)
50 // Mapfile destructor.
52 Mapfile::~Mapfile()
54 if (this->map_file_ != NULL)
55 this->close();
58 // Open the map file.
60 bool
61 Mapfile::open(const char* map_filename)
63 if (strcmp(map_filename, "-") == 0)
64 this->map_file_ = stdout;
65 else
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,
71 strerror(errno));
72 return false;
75 return true;
78 // Close the map file.
80 void
81 Mapfile::close()
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.
90 void
91 Mapfile::advance_to_column(size_t from, size_t to)
93 if (from >= to - 1)
95 putc('\n', this->map_file_);
96 from = 0;
98 while (from < to)
100 putc(' ', this->map_file_);
101 ++from;
105 // Report about including a member from an archive.
107 void
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()
126 + 2);
127 this->advance_to_column(len, 30);
129 if (sym == NULL)
130 fprintf(this->map_file_, "%s", why);
131 else
133 switch (sym->source())
135 case Symbol::FROM_OBJECT:
136 fprintf(this->map_file_, "%s", sym->object()->name().c_str());
137 break;
139 case Symbol::IS_UNDEFINED:
140 fprintf(this->map_file_, "-u");
141 break;
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();
151 fprintf(this->map_file_, " (%s)", sym->name());
154 putc('\n', this->map_file_);
157 // Report allocating a common symbol.
159 void
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);
175 char buf[50];
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);
180 while (len < 18)
182 putc(' ', this->map_file_);
183 ++len;
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.
195 void
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>
208 void
209 Mapfile::print_input_section_symbols(
210 const Sized_relobj<size, big_endian>* relobj,
211 unsigned int shndx)
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);
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())
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());
238 // Print an input section.
240 void
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);
250 Output_section* os;
251 uint64_t addr;
252 if (!relobj->is_section_included(shndx))
254 os = NULL;
255 addr = 0;
257 else
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;
267 char sizebuf[50];
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());
276 if (os != NULL)
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);
287 break;
288 #endif
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);
296 break;
297 #endif
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);
305 break;
306 #endif
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);
314 break;
315 #endif
316 default:
317 gold_unreachable();
322 // Print an Output_section_data. This is printed to look like an
323 // input section.
325 void
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);
336 char sizebuf[50];
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()),
343 sizebuf);
346 // Print the discarded input sections.
348 void
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();
354 ++p)
356 Relobj* relobj = *p;
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))
366 if (!printed_header)
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.
380 void
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);
389 char sizebuf[50];
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.