| 1 | // common.cc -- handle common symbols for gold |
| 2 | |
| 3 | // Copyright 2006, 2007 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 "layout.h" |
| 29 | #include "output.h" |
| 30 | #include "symtab.h" |
| 31 | #include "common.h" |
| 32 | |
| 33 | namespace gold |
| 34 | { |
| 35 | |
| 36 | // Allocate_commons_task methods. |
| 37 | |
| 38 | // This task allocates the common symbols. We need a lock on the |
| 39 | // symbol table. |
| 40 | |
| 41 | Task::Is_runnable_type |
| 42 | Allocate_commons_task::is_runnable(Workqueue*) |
| 43 | { |
| 44 | if (!this->symtab_lock_->is_writable()) |
| 45 | return IS_LOCKED; |
| 46 | return IS_RUNNABLE; |
| 47 | } |
| 48 | |
| 49 | // Return the locks we hold: one on the symbol table, and one blocker. |
| 50 | |
| 51 | class Allocate_commons_task::Allocate_commons_locker : public Task_locker |
| 52 | { |
| 53 | public: |
| 54 | Allocate_commons_locker(Task_token& symtab_lock, Task* task, |
| 55 | Task_token& blocker, Workqueue* workqueue) |
| 56 | : symtab_locker_(symtab_lock, task), |
| 57 | blocker_(blocker, workqueue) |
| 58 | { } |
| 59 | |
| 60 | private: |
| 61 | Task_locker_write symtab_locker_; |
| 62 | Task_locker_block blocker_; |
| 63 | }; |
| 64 | |
| 65 | Task_locker* |
| 66 | Allocate_commons_task::locks(Workqueue* workqueue) |
| 67 | { |
| 68 | return new Allocate_commons_locker(*this->symtab_lock_, this, |
| 69 | *this->blocker_, workqueue); |
| 70 | } |
| 71 | |
| 72 | // Allocate the common symbols. |
| 73 | |
| 74 | void |
| 75 | Allocate_commons_task::run(Workqueue*) |
| 76 | { |
| 77 | this->symtab_->allocate_commons(this->options_, this->layout_); |
| 78 | } |
| 79 | |
| 80 | // This class is used to sort the common symbol by size. We put the |
| 81 | // larger common symbols first. |
| 82 | |
| 83 | template<int size> |
| 84 | class Sort_commons |
| 85 | { |
| 86 | public: |
| 87 | Sort_commons(const Symbol_table* symtab) |
| 88 | : symtab_(symtab) |
| 89 | { } |
| 90 | |
| 91 | bool operator()(const Symbol* a, const Symbol* b) const; |
| 92 | |
| 93 | private: |
| 94 | const Symbol_table* symtab_; |
| 95 | }; |
| 96 | |
| 97 | template<int size> |
| 98 | bool |
| 99 | Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const |
| 100 | { |
| 101 | if (pa == NULL) |
| 102 | return false; |
| 103 | if (pb == NULL) |
| 104 | return true; |
| 105 | |
| 106 | const Symbol_table* symtab = this->symtab_; |
| 107 | const Sized_symbol<size>* psa; |
| 108 | psa = symtab->get_sized_symbol SELECT_SIZE_NAME(size) (pa |
| 109 | SELECT_SIZE(size)); |
| 110 | const Sized_symbol<size>* psb; |
| 111 | psb = symtab->get_sized_symbol SELECT_SIZE_NAME(size) (pb |
| 112 | SELECT_SIZE(size)); |
| 113 | |
| 114 | typename Sized_symbol<size>::Size_type sa = psa->symsize(); |
| 115 | typename Sized_symbol<size>::Size_type sb = psb->symsize(); |
| 116 | if (sa < sb) |
| 117 | return false; |
| 118 | else if (sb > sa) |
| 119 | return true; |
| 120 | |
| 121 | // When the symbols are the same size, we sort them by alignment. |
| 122 | typename Sized_symbol<size>::Value_type va = psa->value(); |
| 123 | typename Sized_symbol<size>::Value_type vb = psb->value(); |
| 124 | if (va < vb) |
| 125 | return false; |
| 126 | else if (vb > va) |
| 127 | return true; |
| 128 | |
| 129 | // Otherwise we stabilize the sort by sorting by name. |
| 130 | return strcmp(psa->name(), psb->name()) < 0; |
| 131 | } |
| 132 | |
| 133 | // Allocate the common symbols. |
| 134 | |
| 135 | void |
| 136 | Symbol_table::allocate_commons(const General_options& options, Layout* layout) |
| 137 | { |
| 138 | if (parameters->get_size() == 32) |
| 139 | { |
| 140 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
| 141 | this->do_allocate_commons<32>(options, layout); |
| 142 | #else |
| 143 | gold_unreachable(); |
| 144 | #endif |
| 145 | } |
| 146 | else if (parameters->get_size() == 64) |
| 147 | { |
| 148 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) |
| 149 | this->do_allocate_commons<64>(options, layout); |
| 150 | #else |
| 151 | gold_unreachable(); |
| 152 | #endif |
| 153 | } |
| 154 | else |
| 155 | gold_unreachable(); |
| 156 | } |
| 157 | |
| 158 | // Allocated the common symbols, sized version. |
| 159 | |
| 160 | template<int size> |
| 161 | void |
| 162 | Symbol_table::do_allocate_commons(const General_options&, |
| 163 | Layout* layout) |
| 164 | { |
| 165 | typedef typename Sized_symbol<size>::Value_type Value_type; |
| 166 | typedef typename Sized_symbol<size>::Size_type Size_type; |
| 167 | |
| 168 | // We've kept a list of all the common symbols. But the symbol may |
| 169 | // have been resolved to a defined symbol by now. And it may be a |
| 170 | // forwarder. First remove all non-common symbols. |
| 171 | bool any = false; |
| 172 | uint64_t addralign = 0; |
| 173 | for (Commons_type::iterator p = this->commons_.begin(); |
| 174 | p != this->commons_.end(); |
| 175 | ++p) |
| 176 | { |
| 177 | Symbol* sym = *p; |
| 178 | if (sym->is_forwarder()) |
| 179 | { |
| 180 | sym = this->resolve_forwards(sym); |
| 181 | *p = sym; |
| 182 | } |
| 183 | if (!sym->is_common()) |
| 184 | *p = NULL; |
| 185 | else |
| 186 | { |
| 187 | any = true; |
| 188 | Sized_symbol<size>* ssym; |
| 189 | ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) ( |
| 190 | sym |
| 191 | SELECT_SIZE(size)); |
| 192 | if (ssym->value() > addralign) |
| 193 | addralign = ssym->value(); |
| 194 | } |
| 195 | } |
| 196 | if (!any) |
| 197 | return; |
| 198 | |
| 199 | // Sort the common symbols by size, so that they pack better into |
| 200 | // memory. |
| 201 | std::sort(this->commons_.begin(), this->commons_.end(), |
| 202 | Sort_commons<size>(this)); |
| 203 | |
| 204 | // Place them in a newly allocated .bss section. |
| 205 | |
| 206 | Output_data_space *poc = new Output_data_space(addralign); |
| 207 | |
| 208 | layout->add_output_section_data(".bss", elfcpp::SHT_NOBITS, |
| 209 | elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC, |
| 210 | poc); |
| 211 | |
| 212 | // Allocate them all. |
| 213 | |
| 214 | off_t off = 0; |
| 215 | for (Commons_type::iterator p = this->commons_.begin(); |
| 216 | p != this->commons_.end(); |
| 217 | ++p) |
| 218 | { |
| 219 | Symbol* sym = *p; |
| 220 | if (sym == NULL) |
| 221 | break; |
| 222 | |
| 223 | Sized_symbol<size>* ssym; |
| 224 | ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) (sym |
| 225 | SELECT_SIZE(size)); |
| 226 | |
| 227 | off = align_address(off, ssym->value()); |
| 228 | |
| 229 | Size_type symsize = ssym->symsize(); |
| 230 | ssym->init(ssym->name(), poc, off, symsize, ssym->type(), |
| 231 | ssym->binding(), ssym->visibility(), ssym->nonvis(), |
| 232 | false); |
| 233 | |
| 234 | off += symsize; |
| 235 | } |
| 236 | |
| 237 | poc->set_space_size(off); |
| 238 | |
| 239 | this->commons_.clear(); |
| 240 | } |
| 241 | |
| 242 | } // End namespace gold. |