1 // common.cc -- handle common symbols for gold 2 3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 4 // Free Software Foundation, Inc. 5 // Written by Ian Lance Taylor <iant@google.com>. 6 7 // This file is part of gold. 8 9 // This program is free software; you can redistribute it and/or modify 10 // it under the terms of the GNU General Public License as published by 11 // the Free Software Foundation; either version 3 of the License, or 12 // (at your option) any later version. 13 14 // This program is distributed in the hope that it will be useful, 15 // but WITHOUT ANY WARRANTY; without even the implied warranty of 16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 // GNU General Public License for more details. 18 19 // You should have received a copy of the GNU General Public License 20 // along with this program; if not, write to the Free Software 21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 22 // MA 02110-1301, USA. 23 24 #include "gold.h" 25 26 #include <algorithm> 27 28 #include "workqueue.h" 29 #include "mapfile.h" 30 #include "layout.h" 31 #include "output.h" 32 #include "symtab.h" 33 #include "common.h" 34 35 namespace gold 36 { 37 38 // Allocate_commons_task methods. 39 40 // This task allocates the common symbols. We arrange to run it 41 // before anything else which needs to access the symbol table. 42 43 Task_token* 44 Allocate_commons_task::is_runnable() 45 { 46 return NULL; 47 } 48 49 // Release a blocker. 50 51 void 52 Allocate_commons_task::locks(Task_locker* tl) 53 { 54 tl->add(this, this->blocker_); 55 } 56 57 // Allocate the common symbols. 58 59 void 60 Allocate_commons_task::run(Workqueue*) 61 { 62 this->symtab_->allocate_commons(this->layout_, this->mapfile_); 63 } 64 65 // This class is used to sort the common symbol. We normally put the 66 // larger common symbols first. This can be changed by using 67 // --sort-commons, which tells the linker to sort by alignment. 68 69 template<int size> 70 class Sort_commons 71 { 72 public: 73 Sort_commons(const Symbol_table* symtab, 74 Symbol_table::Sort_commons_order sort_order) 75 : symtab_(symtab), sort_order_(sort_order) 76 { } 77 78 bool operator()(const Symbol* a, const Symbol* b) const; 79 80 private: 81 // The symbol table. 82 const Symbol_table* symtab_; 83 // How to sort. 84 Symbol_table::Sort_commons_order sort_order_; 85 }; 86 87 template<int size> 88 bool 89 Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const 90 { 91 if (pa == NULL) 92 return false; 93 if (pb == NULL) 94 return true; 95 96 const Symbol_table* symtab = this->symtab_; 97 const Sized_symbol<size>* psa = symtab->get_sized_symbol<size>(pa); 98 const Sized_symbol<size>* psb = symtab->get_sized_symbol<size>(pb); 99 100 // The size. 101 typename Sized_symbol<size>::Size_type sa = psa->symsize(); 102 typename Sized_symbol<size>::Size_type sb = psb->symsize(); 103 104 // The alignment. 105 typename Sized_symbol<size>::Value_type aa = psa->value(); 106 typename Sized_symbol<size>::Value_type ab = psb->value(); 107 108 if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_DESCENDING) 109 { 110 if (aa < ab) 111 return false; 112 else if (ab < aa) 113 return true; 114 } 115 else if (this->sort_order_ 116 == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_ASCENDING) 117 { 118 if (aa < ab) 119 return true; 120 else if (ab < aa) 121 return false; 122 } 123 else 124 gold_assert(this->sort_order_ 125 == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING); 126 127 // Sort by descending size. 128 if (sa < sb) 129 return false; 130 else if (sb < sa) 131 return true; 132 133 if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING) 134 { 135 // When the symbols are the same size, we sort them by 136 // alignment, largest alignment first. 137 if (aa < ab) 138 return false; 139 else if (ab < aa) 140 return true; 141 } 142 143 // Otherwise we stabilize the sort by sorting by name. 144 return strcmp(psa->name(), psb->name()) < 0; 145 } 146 147 // Allocate the common symbols. 148 149 void 150 Symbol_table::allocate_commons(Layout* layout, Mapfile* mapfile) 151 { 152 Sort_commons_order sort_order; 153 if (!parameters->options().user_set_sort_common()) 154 sort_order = SORT_COMMONS_BY_SIZE_DESCENDING; 155 else 156 { 157 const char* order = parameters->options().sort_common(); 158 if (*order == '\0' || strcmp(order, "descending") == 0) 159 sort_order = SORT_COMMONS_BY_ALIGNMENT_DESCENDING; 160 else if (strcmp(order, "ascending") == 0) 161 sort_order = SORT_COMMONS_BY_ALIGNMENT_ASCENDING; 162 else 163 { 164 gold_error("invalid --sort-common argument: %s", order); 165 sort_order = SORT_COMMONS_BY_SIZE_DESCENDING; 166 } 167 } 168 169 if (parameters->target().get_size() == 32) 170 { 171 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) 172 this->do_allocate_commons<32>(layout, mapfile, sort_order); 173 #else 174 gold_unreachable(); 175 #endif 176 } 177 else if (parameters->target().get_size() == 64) 178 { 179 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) 180 this->do_allocate_commons<64>(layout, mapfile, sort_order); 181 #else 182 gold_unreachable(); 183 #endif 184 } 185 else 186 gold_unreachable(); 187 } 188 189 // Allocated the common symbols, sized version. 190 191 template<int size> 192 void 193 Symbol_table::do_allocate_commons(Layout* layout, Mapfile* mapfile, 194 Sort_commons_order sort_order) 195 { 196 if (!this->commons_.empty()) 197 this->do_allocate_commons_list<size>(layout, COMMONS_NORMAL, 198 &this->commons_, mapfile, 199 sort_order); 200 if (!this->tls_commons_.empty()) 201 this->do_allocate_commons_list<size>(layout, COMMONS_TLS, 202 &this->tls_commons_, mapfile, 203 sort_order); 204 if (!this->small_commons_.empty()) 205 this->do_allocate_commons_list<size>(layout, COMMONS_SMALL, 206 &this->small_commons_, mapfile, 207 sort_order); 208 if (!this->large_commons_.empty()) 209 this->do_allocate_commons_list<size>(layout, COMMONS_LARGE, 210 &this->large_commons_, mapfile, 211 sort_order); 212 } 213 214 // Allocate the common symbols in a list. IS_TLS indicates whether 215 // these are TLS common symbols. 216 217 template<int size> 218 void 219 Symbol_table::do_allocate_commons_list( 220 Layout* layout, 221 Commons_section_type commons_section_type, 222 Commons_type* commons, 223 Mapfile* mapfile, 224 Sort_commons_order sort_order) 225 { 226 // We've kept a list of all the common symbols. But the symbol may 227 // have been resolved to a defined symbol by now. And it may be a 228 // forwarder. First remove all non-common symbols. 229 bool any = false; 230 uint64_t addralign = 0; 231 for (Commons_type::iterator p = commons->begin(); 232 p != commons->end(); 233 ++p) 234 { 235 Symbol* sym = *p; 236 if (sym->is_forwarder()) 237 { 238 sym = this->resolve_forwards(sym); 239 *p = sym; 240 } 241 if (!sym->is_common()) 242 *p = NULL; 243 else 244 { 245 any = true; 246 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym); 247 if (ssym->value() > addralign) 248 addralign = ssym->value(); 249 } 250 } 251 if (!any) 252 return; 253 254 // Sort the common symbols. 255 std::sort(commons->begin(), commons->end(), 256 Sort_commons<size>(this, sort_order)); 257 258 // Place them in a newly allocated BSS section. 259 elfcpp::Elf_Xword flags = elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC; 260 const char* name; 261 const char* ds_name; 262 switch (commons_section_type) 263 { 264 case COMMONS_NORMAL: 265 name = ".bss"; 266 ds_name = "** common"; 267 break; 268 case COMMONS_TLS: 269 flags |= elfcpp::SHF_TLS; 270 name = ".tbss"; 271 ds_name = "** tls common"; 272 break; 273 case COMMONS_SMALL: 274 flags |= parameters->target().small_common_section_flags(); 275 name = ".sbss"; 276 ds_name = "** small common"; 277 break; 278 case COMMONS_LARGE: 279 flags |= parameters->target().large_common_section_flags(); 280 name = ".lbss"; 281 ds_name = "** large common"; 282 break; 283 default: 284 gold_unreachable(); 285 } 286 287 Output_data_space* poc; 288 Output_section* os; 289 290 if (!parameters->incremental_update()) 291 { 292 poc = new Output_data_space(addralign, ds_name); 293 os = layout->add_output_section_data(name, elfcpp::SHT_NOBITS, flags, 294 poc, ORDER_INVALID, false); 295 } 296 else 297 { 298 // When doing an incremental update, we need to allocate each common 299 // directly from the output section's free list. 300 poc = NULL; 301 os = layout->find_output_section(name); 302 } 303 304 if (os != NULL) 305 { 306 if (commons_section_type == COMMONS_SMALL) 307 os->set_is_small_section(); 308 else if (commons_section_type == COMMONS_LARGE) 309 os->set_is_large_section(); 310 } 311 312 // Allocate them all. 313 314 off_t off = 0; 315 for (Commons_type::iterator p = commons->begin(); 316 p != commons->end(); 317 ++p) 318 { 319 Symbol* sym = *p; 320 if (sym == NULL) 321 break; 322 323 // Because we followed forwarding symbols above, but we didn't 324 // do it reliably before adding symbols to the list, it is 325 // possible for us to have the same symbol on the list twice. 326 // This can happen in the horrible case where a program defines 327 // a common symbol with the same name as a versioned libc 328 // symbol. That will show up here as a symbol which has already 329 // been allocated and is therefore no longer a common symbol. 330 if (!sym->is_common()) 331 continue; 332 333 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym); 334 335 // Record the symbol in the map file now, before we change its 336 // value. Pass the size in separately so that we don't have to 337 // templatize the map code, which is not performance sensitive. 338 if (mapfile != NULL) 339 mapfile->report_allocate_common(sym, ssym->symsize()); 340 341 if (poc != NULL) 342 { 343 off = align_address(off, ssym->value()); 344 ssym->allocate_common(poc, off); 345 off += ssym->symsize(); 346 } 347 else 348 { 349 // For an incremental update, allocate from the free list. 350 off = os->allocate(ssym->symsize(), ssym->value()); 351 if (off == -1) 352 gold_fallback(_("out of patch space in section %s; " 353 "relink with --incremental-full"), 354 os->name()); 355 ssym->allocate_common(os, off); 356 } 357 } 358 359 if (poc != NULL) 360 poc->set_current_data_size(off); 361 362 commons->clear(); 363 } 364 365 } // End namespace gold. 366