| 1 |
205 |
julius |
// common.cc -- handle common symbols for gold
|
| 2 |
|
|
|
| 3 |
|
|
// Copyright 2006, 2007, 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 <algorithm>
|
| 26 |
|
|
|
| 27 |
|
|
#include "workqueue.h"
|
| 28 |
|
|
#include "mapfile.h"
|
| 29 |
|
|
#include "layout.h"
|
| 30 |
|
|
#include "output.h"
|
| 31 |
|
|
#include "symtab.h"
|
| 32 |
|
|
#include "common.h"
|
| 33 |
|
|
|
| 34 |
|
|
namespace gold
|
| 35 |
|
|
{
|
| 36 |
|
|
|
| 37 |
|
|
// Allocate_commons_task methods.
|
| 38 |
|
|
|
| 39 |
|
|
// This task allocates the common symbols. We need a lock on the
|
| 40 |
|
|
// symbol table.
|
| 41 |
|
|
|
| 42 |
|
|
Task_token*
|
| 43 |
|
|
Allocate_commons_task::is_runnable()
|
| 44 |
|
|
{
|
| 45 |
|
|
if (!this->symtab_lock_->is_writable())
|
| 46 |
|
|
return this->symtab_lock_;
|
| 47 |
|
|
return NULL;
|
| 48 |
|
|
}
|
| 49 |
|
|
|
| 50 |
|
|
// Return the locks we hold: one on the symbol table, and one blocker.
|
| 51 |
|
|
|
| 52 |
|
|
void
|
| 53 |
|
|
Allocate_commons_task::locks(Task_locker* tl)
|
| 54 |
|
|
{
|
| 55 |
|
|
tl->add(this, this->blocker_);
|
| 56 |
|
|
tl->add(this, this->symtab_lock_);
|
| 57 |
|
|
}
|
| 58 |
|
|
|
| 59 |
|
|
// Allocate the common symbols.
|
| 60 |
|
|
|
| 61 |
|
|
void
|
| 62 |
|
|
Allocate_commons_task::run(Workqueue*)
|
| 63 |
|
|
{
|
| 64 |
|
|
this->symtab_->allocate_commons(this->layout_, this->mapfile_);
|
| 65 |
|
|
}
|
| 66 |
|
|
|
| 67 |
|
|
// This class is used to sort the common symbol by size. We put the
|
| 68 |
|
|
// larger common symbols first.
|
| 69 |
|
|
|
| 70 |
|
|
template<int size>
|
| 71 |
|
|
class Sort_commons
|
| 72 |
|
|
{
|
| 73 |
|
|
public:
|
| 74 |
|
|
Sort_commons(const Symbol_table* symtab)
|
| 75 |
|
|
: symtab_(symtab)
|
| 76 |
|
|
{ }
|
| 77 |
|
|
|
| 78 |
|
|
bool operator()(const Symbol* a, const Symbol* b) const;
|
| 79 |
|
|
|
| 80 |
|
|
private:
|
| 81 |
|
|
const Symbol_table* symtab_;
|
| 82 |
|
|
};
|
| 83 |
|
|
|
| 84 |
|
|
template<int size>
|
| 85 |
|
|
bool
|
| 86 |
|
|
Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
|
| 87 |
|
|
{
|
| 88 |
|
|
if (pa == NULL)
|
| 89 |
|
|
return false;
|
| 90 |
|
|
if (pb == NULL)
|
| 91 |
|
|
return true;
|
| 92 |
|
|
|
| 93 |
|
|
const Symbol_table* symtab = this->symtab_;
|
| 94 |
|
|
const Sized_symbol<size>* psa = symtab->get_sized_symbol<size>(pa);
|
| 95 |
|
|
const Sized_symbol<size>* psb = symtab->get_sized_symbol<size>(pb);
|
| 96 |
|
|
|
| 97 |
|
|
// Sort by largest size first.
|
| 98 |
|
|
typename Sized_symbol<size>::Size_type sa = psa->symsize();
|
| 99 |
|
|
typename Sized_symbol<size>::Size_type sb = psb->symsize();
|
| 100 |
|
|
if (sa < sb)
|
| 101 |
|
|
return false;
|
| 102 |
|
|
else if (sb < sa)
|
| 103 |
|
|
return true;
|
| 104 |
|
|
|
| 105 |
|
|
// When the symbols are the same size, we sort them by alignment,
|
| 106 |
|
|
// largest alignment first.
|
| 107 |
|
|
typename Sized_symbol<size>::Value_type va = psa->value();
|
| 108 |
|
|
typename Sized_symbol<size>::Value_type vb = psb->value();
|
| 109 |
|
|
if (va < vb)
|
| 110 |
|
|
return false;
|
| 111 |
|
|
else if (vb < va)
|
| 112 |
|
|
return true;
|
| 113 |
|
|
|
| 114 |
|
|
// Otherwise we stabilize the sort by sorting by name.
|
| 115 |
|
|
return strcmp(psa->name(), psb->name()) < 0;
|
| 116 |
|
|
}
|
| 117 |
|
|
|
| 118 |
|
|
// Allocate the common symbols.
|
| 119 |
|
|
|
| 120 |
|
|
void
|
| 121 |
|
|
Symbol_table::allocate_commons(Layout* layout, Mapfile* mapfile)
|
| 122 |
|
|
{
|
| 123 |
|
|
if (parameters->target().get_size() == 32)
|
| 124 |
|
|
{
|
| 125 |
|
|
#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
|
| 126 |
|
|
this->do_allocate_commons<32>(layout, mapfile);
|
| 127 |
|
|
#else
|
| 128 |
|
|
gold_unreachable();
|
| 129 |
|
|
#endif
|
| 130 |
|
|
}
|
| 131 |
|
|
else if (parameters->target().get_size() == 64)
|
| 132 |
|
|
{
|
| 133 |
|
|
#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
|
| 134 |
|
|
this->do_allocate_commons<64>(layout, mapfile);
|
| 135 |
|
|
#else
|
| 136 |
|
|
gold_unreachable();
|
| 137 |
|
|
#endif
|
| 138 |
|
|
}
|
| 139 |
|
|
else
|
| 140 |
|
|
gold_unreachable();
|
| 141 |
|
|
}
|
| 142 |
|
|
|
| 143 |
|
|
// Allocated the common symbols, sized version.
|
| 144 |
|
|
|
| 145 |
|
|
template<int size>
|
| 146 |
|
|
void
|
| 147 |
|
|
Symbol_table::do_allocate_commons(Layout* layout, Mapfile* mapfile)
|
| 148 |
|
|
{
|
| 149 |
|
|
if (!this->commons_.empty())
|
| 150 |
|
|
this->do_allocate_commons_list<size>(layout, COMMONS_NORMAL,
|
| 151 |
|
|
&this->commons_, mapfile);
|
| 152 |
|
|
if (!this->tls_commons_.empty())
|
| 153 |
|
|
this->do_allocate_commons_list<size>(layout, COMMONS_TLS,
|
| 154 |
|
|
&this->tls_commons_, mapfile);
|
| 155 |
|
|
if (!this->small_commons_.empty())
|
| 156 |
|
|
this->do_allocate_commons_list<size>(layout, COMMONS_SMALL,
|
| 157 |
|
|
&this->small_commons_, mapfile);
|
| 158 |
|
|
if (!this->large_commons_.empty())
|
| 159 |
|
|
this->do_allocate_commons_list<size>(layout, COMMONS_LARGE,
|
| 160 |
|
|
&this->large_commons_, mapfile);
|
| 161 |
|
|
}
|
| 162 |
|
|
|
| 163 |
|
|
// Allocate the common symbols in a list. IS_TLS indicates whether
|
| 164 |
|
|
// these are TLS common symbols.
|
| 165 |
|
|
|
| 166 |
|
|
template<int size>
|
| 167 |
|
|
void
|
| 168 |
|
|
Symbol_table::do_allocate_commons_list(
|
| 169 |
|
|
Layout* layout,
|
| 170 |
|
|
Commons_section_type commons_section_type,
|
| 171 |
|
|
Commons_type* commons,
|
| 172 |
|
|
Mapfile* mapfile)
|
| 173 |
|
|
{
|
| 174 |
|
|
typedef typename Sized_symbol<size>::Value_type Value_type;
|
| 175 |
|
|
typedef typename Sized_symbol<size>::Size_type Size_type;
|
| 176 |
|
|
|
| 177 |
|
|
// We've kept a list of all the common symbols. But the symbol may
|
| 178 |
|
|
// have been resolved to a defined symbol by now. And it may be a
|
| 179 |
|
|
// forwarder. First remove all non-common symbols.
|
| 180 |
|
|
bool any = false;
|
| 181 |
|
|
uint64_t addralign = 0;
|
| 182 |
|
|
for (Commons_type::iterator p = commons->begin();
|
| 183 |
|
|
p != commons->end();
|
| 184 |
|
|
++p)
|
| 185 |
|
|
{
|
| 186 |
|
|
Symbol* sym = *p;
|
| 187 |
|
|
if (sym->is_forwarder())
|
| 188 |
|
|
{
|
| 189 |
|
|
sym = this->resolve_forwards(sym);
|
| 190 |
|
|
*p = sym;
|
| 191 |
|
|
}
|
| 192 |
|
|
if (!sym->is_common())
|
| 193 |
|
|
*p = NULL;
|
| 194 |
|
|
else
|
| 195 |
|
|
{
|
| 196 |
|
|
any = true;
|
| 197 |
|
|
Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
|
| 198 |
|
|
if (ssym->value() > addralign)
|
| 199 |
|
|
addralign = ssym->value();
|
| 200 |
|
|
}
|
| 201 |
|
|
}
|
| 202 |
|
|
if (!any)
|
| 203 |
|
|
return;
|
| 204 |
|
|
|
| 205 |
|
|
// Sort the common symbols by size, so that they pack better into
|
| 206 |
|
|
// memory.
|
| 207 |
|
|
std::sort(commons->begin(), commons->end(),
|
| 208 |
|
|
Sort_commons<size>(this));
|
| 209 |
|
|
|
| 210 |
|
|
// Place them in a newly allocated BSS section.
|
| 211 |
|
|
elfcpp::Elf_Xword flags = elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC;
|
| 212 |
|
|
const char* name;
|
| 213 |
|
|
const char* ds_name;
|
| 214 |
|
|
switch (commons_section_type)
|
| 215 |
|
|
{
|
| 216 |
|
|
case COMMONS_NORMAL:
|
| 217 |
|
|
name = ".bss";
|
| 218 |
|
|
ds_name = "** common";
|
| 219 |
|
|
break;
|
| 220 |
|
|
case COMMONS_TLS:
|
| 221 |
|
|
flags |= elfcpp::SHF_TLS;
|
| 222 |
|
|
name = ".tbss";
|
| 223 |
|
|
ds_name = "** tls common";
|
| 224 |
|
|
break;
|
| 225 |
|
|
case COMMONS_SMALL:
|
| 226 |
|
|
flags |= parameters->target().small_common_section_flags();
|
| 227 |
|
|
name = ".sbss";
|
| 228 |
|
|
ds_name = "** small common";
|
| 229 |
|
|
break;
|
| 230 |
|
|
case COMMONS_LARGE:
|
| 231 |
|
|
flags |= parameters->target().large_common_section_flags();
|
| 232 |
|
|
name = ".lbss";
|
| 233 |
|
|
ds_name = "** large common";
|
| 234 |
|
|
break;
|
| 235 |
|
|
default:
|
| 236 |
|
|
gold_unreachable();
|
| 237 |
|
|
}
|
| 238 |
|
|
|
| 239 |
|
|
Output_data_space *poc = new Output_data_space(addralign, ds_name);
|
| 240 |
|
|
Output_section *os = layout->add_output_section_data(name,
|
| 241 |
|
|
elfcpp::SHT_NOBITS,
|
| 242 |
|
|
flags, poc, false);
|
| 243 |
|
|
if (os != NULL)
|
| 244 |
|
|
{
|
| 245 |
|
|
if (commons_section_type == COMMONS_SMALL)
|
| 246 |
|
|
os->set_is_small_section();
|
| 247 |
|
|
else if (commons_section_type == COMMONS_LARGE)
|
| 248 |
|
|
os->set_is_large_section();
|
| 249 |
|
|
}
|
| 250 |
|
|
|
| 251 |
|
|
// Allocate them all.
|
| 252 |
|
|
|
| 253 |
|
|
off_t off = 0;
|
| 254 |
|
|
for (Commons_type::iterator p = commons->begin();
|
| 255 |
|
|
p != commons->end();
|
| 256 |
|
|
++p)
|
| 257 |
|
|
{
|
| 258 |
|
|
Symbol* sym = *p;
|
| 259 |
|
|
if (sym == NULL)
|
| 260 |
|
|
break;
|
| 261 |
|
|
Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
|
| 262 |
|
|
|
| 263 |
|
|
// Record the symbol in the map file now, before we change its
|
| 264 |
|
|
// value. Pass the size in separately so that we don't have to
|
| 265 |
|
|
// templatize the map code, which is not performance sensitive.
|
| 266 |
|
|
if (mapfile != NULL)
|
| 267 |
|
|
mapfile->report_allocate_common(sym, ssym->symsize());
|
| 268 |
|
|
|
| 269 |
|
|
off = align_address(off, ssym->value());
|
| 270 |
|
|
ssym->allocate_common(poc, off);
|
| 271 |
|
|
off += ssym->symsize();
|
| 272 |
|
|
}
|
| 273 |
|
|
|
| 274 |
|
|
poc->set_current_data_size(off);
|
| 275 |
|
|
|
| 276 |
|
|
commons->clear();
|
| 277 |
|
|
}
|
| 278 |
|
|
|
| 279 |
|
|
} // End namespace gold.
|