| 1 | // fileread.cc -- read files for gold |
| 2 | |
| 3 | // Copyright 2006, 2007, 2008, 2009 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 <cstring> |
| 26 | #include <cerrno> |
| 27 | #include <fcntl.h> |
| 28 | #include <unistd.h> |
| 29 | #include <sys/mman.h> |
| 30 | #include <sys/uio.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include "filenames.h" |
| 33 | |
| 34 | #include "debug.h" |
| 35 | #include "parameters.h" |
| 36 | #include "options.h" |
| 37 | #include "dirsearch.h" |
| 38 | #include "target.h" |
| 39 | #include "binary.h" |
| 40 | #include "descriptors.h" |
| 41 | #include "fileread.h" |
| 42 | |
| 43 | #ifndef HAVE_READV |
| 44 | struct iovec { void* iov_base; size_t iov_len }; |
| 45 | ssize_t |
| 46 | readv(int, const iovec*, int) |
| 47 | { |
| 48 | gold_unreachable(); |
| 49 | } |
| 50 | #endif |
| 51 | |
| 52 | namespace gold |
| 53 | { |
| 54 | |
| 55 | // Class File_read::View. |
| 56 | |
| 57 | File_read::View::~View() |
| 58 | { |
| 59 | gold_assert(!this->is_locked()); |
| 60 | switch (this->data_ownership_) |
| 61 | { |
| 62 | case DATA_ALLOCATED_ARRAY: |
| 63 | delete[] this->data_; |
| 64 | break; |
| 65 | case DATA_MMAPPED: |
| 66 | if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0) |
| 67 | gold_warning(_("munmap failed: %s"), strerror(errno)); |
| 68 | File_read::current_mapped_bytes -= this->size_; |
| 69 | break; |
| 70 | case DATA_NOT_OWNED: |
| 71 | break; |
| 72 | default: |
| 73 | gold_unreachable(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | File_read::View::lock() |
| 79 | { |
| 80 | ++this->lock_count_; |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | File_read::View::unlock() |
| 85 | { |
| 86 | gold_assert(this->lock_count_ > 0); |
| 87 | --this->lock_count_; |
| 88 | } |
| 89 | |
| 90 | bool |
| 91 | File_read::View::is_locked() |
| 92 | { |
| 93 | return this->lock_count_ > 0; |
| 94 | } |
| 95 | |
| 96 | // Class File_read. |
| 97 | |
| 98 | // The File_read static variables. |
| 99 | unsigned long long File_read::total_mapped_bytes; |
| 100 | unsigned long long File_read::current_mapped_bytes; |
| 101 | unsigned long long File_read::maximum_mapped_bytes; |
| 102 | |
| 103 | File_read::~File_read() |
| 104 | { |
| 105 | gold_assert(this->token_.is_writable()); |
| 106 | if (this->is_descriptor_opened_) |
| 107 | { |
| 108 | release_descriptor(this->descriptor_, true); |
| 109 | this->descriptor_ = -1; |
| 110 | this->is_descriptor_opened_ = false; |
| 111 | } |
| 112 | this->name_.clear(); |
| 113 | this->clear_views(true); |
| 114 | if (this->whole_file_view_) |
| 115 | delete this->whole_file_view_; |
| 116 | } |
| 117 | |
| 118 | // Open the file. |
| 119 | |
| 120 | bool |
| 121 | File_read::open(const Task* task, const std::string& name) |
| 122 | { |
| 123 | gold_assert(this->token_.is_writable() |
| 124 | && this->descriptor_ < 0 |
| 125 | && !this->is_descriptor_opened_ |
| 126 | && this->name_.empty()); |
| 127 | this->name_ = name; |
| 128 | |
| 129 | this->descriptor_ = open_descriptor(-1, this->name_.c_str(), |
| 130 | O_RDONLY); |
| 131 | |
| 132 | if (this->descriptor_ >= 0) |
| 133 | { |
| 134 | this->is_descriptor_opened_ = true; |
| 135 | struct stat s; |
| 136 | if (::fstat(this->descriptor_, &s) < 0) |
| 137 | gold_error(_("%s: fstat failed: %s"), |
| 138 | this->name_.c_str(), strerror(errno)); |
| 139 | this->size_ = s.st_size; |
| 140 | gold_debug(DEBUG_FILES, "Attempt to open %s succeeded", |
| 141 | this->name_.c_str()); |
| 142 | |
| 143 | // Options may not yet be ready e.g. when reading a version |
| 144 | // script. We then default to --no-keep-files-mapped. |
| 145 | if (parameters->options_valid() |
| 146 | && parameters->options().keep_files_mapped()) |
| 147 | { |
| 148 | const unsigned char* contents = static_cast<const unsigned char*>( |
| 149 | ::mmap(NULL, this->size_, PROT_READ, MAP_PRIVATE, |
| 150 | this->descriptor_, 0)); |
| 151 | if (contents == MAP_FAILED) |
| 152 | gold_fatal(_("%s: mmap failed: %s"), this->filename().c_str(), |
| 153 | strerror(errno)); |
| 154 | this->whole_file_view_ = new View(0, this->size_, contents, 0, false, |
| 155 | View::DATA_MMAPPED); |
| 156 | this->mapped_bytes_ += this->size_; |
| 157 | } |
| 158 | |
| 159 | this->token_.add_writer(task); |
| 160 | } |
| 161 | |
| 162 | return this->descriptor_ >= 0; |
| 163 | } |
| 164 | |
| 165 | // Open the file with the contents in memory. |
| 166 | |
| 167 | bool |
| 168 | File_read::open(const Task* task, const std::string& name, |
| 169 | const unsigned char* contents, off_t size) |
| 170 | { |
| 171 | gold_assert(this->token_.is_writable() |
| 172 | && this->descriptor_ < 0 |
| 173 | && !this->is_descriptor_opened_ |
| 174 | && this->name_.empty()); |
| 175 | this->name_ = name; |
| 176 | this->whole_file_view_ = new View(0, size, contents, 0, false, |
| 177 | View::DATA_NOT_OWNED); |
| 178 | this->size_ = size; |
| 179 | this->token_.add_writer(task); |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | // Reopen a descriptor if necessary. |
| 184 | |
| 185 | void |
| 186 | File_read::reopen_descriptor() |
| 187 | { |
| 188 | if (!this->is_descriptor_opened_) |
| 189 | { |
| 190 | this->descriptor_ = open_descriptor(this->descriptor_, |
| 191 | this->name_.c_str(), |
| 192 | O_RDONLY); |
| 193 | if (this->descriptor_ < 0) |
| 194 | gold_fatal(_("could not reopen file %s"), this->name_.c_str()); |
| 195 | this->is_descriptor_opened_ = true; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // Release the file. This is called when we are done with the file in |
| 200 | // a Task. |
| 201 | |
| 202 | void |
| 203 | File_read::release() |
| 204 | { |
| 205 | gold_assert(this->is_locked()); |
| 206 | |
| 207 | File_read::total_mapped_bytes += this->mapped_bytes_; |
| 208 | File_read::current_mapped_bytes += this->mapped_bytes_; |
| 209 | this->mapped_bytes_ = 0; |
| 210 | if (File_read::current_mapped_bytes > File_read::maximum_mapped_bytes) |
| 211 | File_read::maximum_mapped_bytes = File_read::current_mapped_bytes; |
| 212 | |
| 213 | // Only clear views if there is only one attached object. Otherwise |
| 214 | // we waste time trying to clear cached archive views. Similarly |
| 215 | // for releasing the descriptor. |
| 216 | if (this->object_count_ <= 1) |
| 217 | { |
| 218 | this->clear_views(false); |
| 219 | if (this->is_descriptor_opened_) |
| 220 | { |
| 221 | release_descriptor(this->descriptor_, false); |
| 222 | this->is_descriptor_opened_ = false; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | this->released_ = true; |
| 227 | } |
| 228 | |
| 229 | // Lock the file. |
| 230 | |
| 231 | void |
| 232 | File_read::lock(const Task* task) |
| 233 | { |
| 234 | gold_assert(this->released_); |
| 235 | this->token_.add_writer(task); |
| 236 | this->released_ = false; |
| 237 | } |
| 238 | |
| 239 | // Unlock the file. |
| 240 | |
| 241 | void |
| 242 | File_read::unlock(const Task* task) |
| 243 | { |
| 244 | this->release(); |
| 245 | this->token_.remove_writer(task); |
| 246 | } |
| 247 | |
| 248 | // Return whether the file is locked. |
| 249 | |
| 250 | bool |
| 251 | File_read::is_locked() const |
| 252 | { |
| 253 | if (!this->token_.is_writable()) |
| 254 | return true; |
| 255 | // The file is not locked, so it should have been released. |
| 256 | gold_assert(this->released_); |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | // See if we have a view which covers the file starting at START for |
| 261 | // SIZE bytes. Return a pointer to the View if found, NULL if not. |
| 262 | // If BYTESHIFT is not -1U, the returned View must have the specified |
| 263 | // byte shift; otherwise, it may have any byte shift. If VSHIFTED is |
| 264 | // not NULL, this sets *VSHIFTED to a view which would have worked if |
| 265 | // not for the requested BYTESHIFT. |
| 266 | |
| 267 | inline File_read::View* |
| 268 | File_read::find_view(off_t start, section_size_type size, |
| 269 | unsigned int byteshift, File_read::View** vshifted) const |
| 270 | { |
| 271 | if (vshifted != NULL) |
| 272 | *vshifted = NULL; |
| 273 | |
| 274 | // If we have the whole file mmapped, and the alignment is right, |
| 275 | // we can return it. |
| 276 | if (this->whole_file_view_) |
| 277 | if (byteshift == -1U || byteshift == 0) |
| 278 | return this->whole_file_view_; |
| 279 | |
| 280 | off_t page = File_read::page_offset(start); |
| 281 | |
| 282 | unsigned int bszero = 0; |
| 283 | Views::const_iterator p = this->views_.upper_bound(std::make_pair(page - 1, |
| 284 | bszero)); |
| 285 | |
| 286 | while (p != this->views_.end() && p->first.first <= page) |
| 287 | { |
| 288 | if (p->second->start() <= start |
| 289 | && (p->second->start() + static_cast<off_t>(p->second->size()) |
| 290 | >= start + static_cast<off_t>(size))) |
| 291 | { |
| 292 | if (byteshift == -1U || byteshift == p->second->byteshift()) |
| 293 | { |
| 294 | p->second->set_accessed(); |
| 295 | return p->second; |
| 296 | } |
| 297 | |
| 298 | if (vshifted != NULL && *vshifted == NULL) |
| 299 | *vshifted = p->second; |
| 300 | } |
| 301 | |
| 302 | ++p; |
| 303 | } |
| 304 | |
| 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | // Read SIZE bytes from the file starting at offset START. Read into |
| 309 | // the buffer at P. |
| 310 | |
| 311 | void |
| 312 | File_read::do_read(off_t start, section_size_type size, void* p) |
| 313 | { |
| 314 | ssize_t bytes; |
| 315 | if (this->whole_file_view_ != NULL) |
| 316 | { |
| 317 | bytes = this->size_ - start; |
| 318 | if (static_cast<section_size_type>(bytes) >= size) |
| 319 | { |
| 320 | memcpy(p, this->whole_file_view_->data() + start, size); |
| 321 | return; |
| 322 | } |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | this->reopen_descriptor(); |
| 327 | bytes = ::pread(this->descriptor_, p, size, start); |
| 328 | if (static_cast<section_size_type>(bytes) == size) |
| 329 | return; |
| 330 | |
| 331 | if (bytes < 0) |
| 332 | { |
| 333 | gold_fatal(_("%s: pread failed: %s"), |
| 334 | this->filename().c_str(), strerror(errno)); |
| 335 | return; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | gold_fatal(_("%s: file too short: read only %lld of %lld bytes at %lld"), |
| 340 | this->filename().c_str(), |
| 341 | static_cast<long long>(bytes), |
| 342 | static_cast<long long>(size), |
| 343 | static_cast<long long>(start)); |
| 344 | } |
| 345 | |
| 346 | // Read data from the file. |
| 347 | |
| 348 | void |
| 349 | File_read::read(off_t start, section_size_type size, void* p) |
| 350 | { |
| 351 | const File_read::View* pv = this->find_view(start, size, -1U, NULL); |
| 352 | if (pv != NULL) |
| 353 | { |
| 354 | memcpy(p, pv->data() + (start - pv->start() + pv->byteshift()), size); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | this->do_read(start, size, p); |
| 359 | } |
| 360 | |
| 361 | // Add a new view. There may already be an existing view at this |
| 362 | // offset. If there is, the new view will be larger, and should |
| 363 | // replace the old view. |
| 364 | |
| 365 | void |
| 366 | File_read::add_view(File_read::View* v) |
| 367 | { |
| 368 | std::pair<Views::iterator, bool> ins = |
| 369 | this->views_.insert(std::make_pair(std::make_pair(v->start(), |
| 370 | v->byteshift()), |
| 371 | v)); |
| 372 | if (ins.second) |
| 373 | return; |
| 374 | |
| 375 | // There was an existing view at this offset. It must not be large |
| 376 | // enough. We can't delete it here, since something might be using |
| 377 | // it; we put it on a list to be deleted when the file is unlocked. |
| 378 | File_read::View* vold = ins.first->second; |
| 379 | gold_assert(vold->size() < v->size()); |
| 380 | if (vold->should_cache()) |
| 381 | { |
| 382 | v->set_cache(); |
| 383 | vold->clear_cache(); |
| 384 | } |
| 385 | this->saved_views_.push_back(vold); |
| 386 | |
| 387 | ins.first->second = v; |
| 388 | } |
| 389 | |
| 390 | // Make a new view with a specified byteshift, reading the data from |
| 391 | // the file. |
| 392 | |
| 393 | File_read::View* |
| 394 | File_read::make_view(off_t start, section_size_type size, |
| 395 | unsigned int byteshift, bool cache) |
| 396 | { |
| 397 | gold_assert(size > 0); |
| 398 | |
| 399 | // Check that start and end of the view are within the file. |
| 400 | if (start > this->size_ |
| 401 | || (static_cast<unsigned long long>(size) |
| 402 | > static_cast<unsigned long long>(this->size_ - start))) |
| 403 | gold_fatal(_("%s: attempt to map %lld bytes at offset %lld exceeds " |
| 404 | "size of file; the file may be corrupt"), |
| 405 | this->filename().c_str(), |
| 406 | static_cast<long long>(size), |
| 407 | static_cast<long long>(start)); |
| 408 | |
| 409 | off_t poff = File_read::page_offset(start); |
| 410 | |
| 411 | section_size_type psize = File_read::pages(size + (start - poff)); |
| 412 | |
| 413 | if (poff + static_cast<off_t>(psize) >= this->size_) |
| 414 | { |
| 415 | psize = this->size_ - poff; |
| 416 | gold_assert(psize >= size); |
| 417 | } |
| 418 | |
| 419 | File_read::View* v; |
| 420 | if (this->whole_file_view_ != NULL || byteshift != 0) |
| 421 | { |
| 422 | unsigned char* p = new unsigned char[psize + byteshift]; |
| 423 | memset(p, 0, byteshift); |
| 424 | this->do_read(poff, psize, p + byteshift); |
| 425 | v = new File_read::View(poff, psize, p, byteshift, cache, |
| 426 | View::DATA_ALLOCATED_ARRAY); |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | this->reopen_descriptor(); |
| 431 | void* p = ::mmap(NULL, psize, PROT_READ, MAP_PRIVATE, |
| 432 | this->descriptor_, poff); |
| 433 | if (p == MAP_FAILED) |
| 434 | gold_fatal(_("%s: mmap offset %lld size %lld failed: %s"), |
| 435 | this->filename().c_str(), |
| 436 | static_cast<long long>(poff), |
| 437 | static_cast<long long>(psize), |
| 438 | strerror(errno)); |
| 439 | |
| 440 | this->mapped_bytes_ += psize; |
| 441 | |
| 442 | const unsigned char* pbytes = static_cast<const unsigned char*>(p); |
| 443 | v = new File_read::View(poff, psize, pbytes, 0, cache, |
| 444 | View::DATA_MMAPPED); |
| 445 | } |
| 446 | |
| 447 | this->add_view(v); |
| 448 | |
| 449 | return v; |
| 450 | } |
| 451 | |
| 452 | // Find a View or make a new one, shifted as required by the file |
| 453 | // offset OFFSET and ALIGNED. |
| 454 | |
| 455 | File_read::View* |
| 456 | File_read::find_or_make_view(off_t offset, off_t start, |
| 457 | section_size_type size, bool aligned, bool cache) |
| 458 | { |
| 459 | unsigned int byteshift; |
| 460 | if (offset == 0) |
| 461 | byteshift = 0; |
| 462 | else |
| 463 | { |
| 464 | unsigned int target_size = (!parameters->target_valid() |
| 465 | ? 64 |
| 466 | : parameters->target().get_size()); |
| 467 | byteshift = offset & ((target_size / 8) - 1); |
| 468 | |
| 469 | // Set BYTESHIFT to the number of dummy bytes which must be |
| 470 | // inserted before the data in order for this data to be |
| 471 | // aligned. |
| 472 | if (byteshift != 0) |
| 473 | byteshift = (target_size / 8) - byteshift; |
| 474 | } |
| 475 | |
| 476 | // Try to find a View with the required BYTESHIFT. |
| 477 | File_read::View* vshifted; |
| 478 | File_read::View* v = this->find_view(offset + start, size, |
| 479 | aligned ? byteshift : -1U, |
| 480 | &vshifted); |
| 481 | if (v != NULL) |
| 482 | { |
| 483 | if (cache) |
| 484 | v->set_cache(); |
| 485 | return v; |
| 486 | } |
| 487 | |
| 488 | // If VSHIFTED is not NULL, then it has the data we need, but with |
| 489 | // the wrong byteshift. |
| 490 | v = vshifted; |
| 491 | if (v != NULL) |
| 492 | { |
| 493 | gold_assert(aligned); |
| 494 | |
| 495 | unsigned char* pbytes = new unsigned char[v->size() + byteshift]; |
| 496 | memset(pbytes, 0, byteshift); |
| 497 | memcpy(pbytes + byteshift, v->data() + v->byteshift(), v->size()); |
| 498 | |
| 499 | File_read::View* shifted_view = |
| 500 | new File_read::View(v->start(), v->size(), pbytes, byteshift, |
| 501 | cache, View::DATA_ALLOCATED_ARRAY); |
| 502 | |
| 503 | this->add_view(shifted_view); |
| 504 | return shifted_view; |
| 505 | } |
| 506 | |
| 507 | // Make a new view. If we don't need an aligned view, use a |
| 508 | // byteshift of 0, so that we can use mmap. |
| 509 | return this->make_view(offset + start, size, |
| 510 | aligned ? byteshift : 0, |
| 511 | cache); |
| 512 | } |
| 513 | |
| 514 | // Get a view into the file. |
| 515 | |
| 516 | const unsigned char* |
| 517 | File_read::get_view(off_t offset, off_t start, section_size_type size, |
| 518 | bool aligned, bool cache) |
| 519 | { |
| 520 | File_read::View* pv = this->find_or_make_view(offset, start, size, |
| 521 | aligned, cache); |
| 522 | return pv->data() + (offset + start - pv->start() + pv->byteshift()); |
| 523 | } |
| 524 | |
| 525 | File_view* |
| 526 | File_read::get_lasting_view(off_t offset, off_t start, section_size_type size, |
| 527 | bool aligned, bool cache) |
| 528 | { |
| 529 | File_read::View* pv = this->find_or_make_view(offset, start, size, |
| 530 | aligned, cache); |
| 531 | pv->lock(); |
| 532 | return new File_view(*this, pv, |
| 533 | (pv->data() |
| 534 | + (offset + start - pv->start() + pv->byteshift()))); |
| 535 | } |
| 536 | |
| 537 | // Use readv to read COUNT entries from RM starting at START. BASE |
| 538 | // must be added to all file offsets in RM. |
| 539 | |
| 540 | void |
| 541 | File_read::do_readv(off_t base, const Read_multiple& rm, size_t start, |
| 542 | size_t count) |
| 543 | { |
| 544 | unsigned char discard[File_read::page_size]; |
| 545 | iovec iov[File_read::max_readv_entries * 2]; |
| 546 | size_t iov_index = 0; |
| 547 | |
| 548 | off_t first_offset = rm[start].file_offset; |
| 549 | off_t last_offset = first_offset; |
| 550 | ssize_t want = 0; |
| 551 | for (size_t i = 0; i < count; ++i) |
| 552 | { |
| 553 | const Read_multiple_entry& i_entry(rm[start + i]); |
| 554 | |
| 555 | if (i_entry.file_offset > last_offset) |
| 556 | { |
| 557 | size_t skip = i_entry.file_offset - last_offset; |
| 558 | gold_assert(skip <= sizeof discard); |
| 559 | |
| 560 | iov[iov_index].iov_base = discard; |
| 561 | iov[iov_index].iov_len = skip; |
| 562 | ++iov_index; |
| 563 | |
| 564 | want += skip; |
| 565 | } |
| 566 | |
| 567 | iov[iov_index].iov_base = i_entry.buffer; |
| 568 | iov[iov_index].iov_len = i_entry.size; |
| 569 | ++iov_index; |
| 570 | |
| 571 | want += i_entry.size; |
| 572 | |
| 573 | last_offset = i_entry.file_offset + i_entry.size; |
| 574 | } |
| 575 | |
| 576 | this->reopen_descriptor(); |
| 577 | |
| 578 | gold_assert(iov_index < sizeof iov / sizeof iov[0]); |
| 579 | |
| 580 | if (::lseek(this->descriptor_, base + first_offset, SEEK_SET) < 0) |
| 581 | gold_fatal(_("%s: lseek failed: %s"), |
| 582 | this->filename().c_str(), strerror(errno)); |
| 583 | |
| 584 | ssize_t got = ::readv(this->descriptor_, iov, iov_index); |
| 585 | |
| 586 | if (got < 0) |
| 587 | gold_fatal(_("%s: readv failed: %s"), |
| 588 | this->filename().c_str(), strerror(errno)); |
| 589 | if (got != want) |
| 590 | gold_fatal(_("%s: file too short: read only %zd of %zd bytes at %lld"), |
| 591 | this->filename().c_str(), |
| 592 | got, want, static_cast<long long>(base + first_offset)); |
| 593 | } |
| 594 | |
| 595 | // Read several pieces of data from the file. |
| 596 | |
| 597 | void |
| 598 | File_read::read_multiple(off_t base, const Read_multiple& rm) |
| 599 | { |
| 600 | size_t count = rm.size(); |
| 601 | size_t i = 0; |
| 602 | while (i < count) |
| 603 | { |
| 604 | // Find up to MAX_READV_ENTRIES consecutive entries which are |
| 605 | // less than one page apart. |
| 606 | const Read_multiple_entry& i_entry(rm[i]); |
| 607 | off_t i_off = i_entry.file_offset; |
| 608 | off_t end_off = i_off + i_entry.size; |
| 609 | size_t j; |
| 610 | for (j = i + 1; j < count; ++j) |
| 611 | { |
| 612 | if (j - i >= File_read::max_readv_entries) |
| 613 | break; |
| 614 | const Read_multiple_entry& j_entry(rm[j]); |
| 615 | off_t j_off = j_entry.file_offset; |
| 616 | gold_assert(j_off >= end_off); |
| 617 | off_t j_end_off = j_off + j_entry.size; |
| 618 | if (j_end_off - end_off >= File_read::page_size) |
| 619 | break; |
| 620 | end_off = j_end_off; |
| 621 | } |
| 622 | |
| 623 | if (j == i + 1) |
| 624 | this->read(base + i_off, i_entry.size, i_entry.buffer); |
| 625 | else |
| 626 | { |
| 627 | File_read::View* view = this->find_view(base + i_off, |
| 628 | end_off - i_off, |
| 629 | -1U, NULL); |
| 630 | if (view == NULL) |
| 631 | this->do_readv(base, rm, i, j - i); |
| 632 | else |
| 633 | { |
| 634 | const unsigned char* v = (view->data() |
| 635 | + (base + i_off - view->start() |
| 636 | + view->byteshift())); |
| 637 | for (size_t k = i; k < j; ++k) |
| 638 | { |
| 639 | const Read_multiple_entry& k_entry(rm[k]); |
| 640 | gold_assert((convert_to_section_size_type(k_entry.file_offset |
| 641 | - i_off) |
| 642 | + k_entry.size) |
| 643 | <= convert_to_section_size_type(end_off |
| 644 | - i_off)); |
| 645 | memcpy(k_entry.buffer, |
| 646 | v + (k_entry.file_offset - i_off), |
| 647 | k_entry.size); |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | i = j; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | // Mark all views as no longer cached. |
| 657 | |
| 658 | void |
| 659 | File_read::clear_view_cache_marks() |
| 660 | { |
| 661 | // Just ignore this if there are multiple objects associated with |
| 662 | // the file. Otherwise we will wind up uncaching and freeing some |
| 663 | // views for other objects. |
| 664 | if (this->object_count_ > 1) |
| 665 | return; |
| 666 | |
| 667 | for (Views::iterator p = this->views_.begin(); |
| 668 | p != this->views_.end(); |
| 669 | ++p) |
| 670 | p->second->clear_cache(); |
| 671 | for (Saved_views::iterator p = this->saved_views_.begin(); |
| 672 | p != this->saved_views_.end(); |
| 673 | ++p) |
| 674 | (*p)->clear_cache(); |
| 675 | } |
| 676 | |
| 677 | // Remove all the file views. For a file which has multiple |
| 678 | // associated objects (i.e., an archive), we keep accessed views |
| 679 | // around until next time, in the hopes that they will be useful for |
| 680 | // the next object. |
| 681 | |
| 682 | void |
| 683 | File_read::clear_views(bool destroying) |
| 684 | { |
| 685 | Views::iterator p = this->views_.begin(); |
| 686 | while (p != this->views_.end()) |
| 687 | { |
| 688 | bool should_delete; |
| 689 | if (p->second->is_locked()) |
| 690 | should_delete = false; |
| 691 | else if (destroying) |
| 692 | should_delete = true; |
| 693 | else if (p->second->should_cache()) |
| 694 | should_delete = false; |
| 695 | else if (this->object_count_ > 1 && p->second->accessed()) |
| 696 | should_delete = false; |
| 697 | else |
| 698 | should_delete = true; |
| 699 | |
| 700 | if (should_delete) |
| 701 | { |
| 702 | delete p->second; |
| 703 | |
| 704 | // map::erase invalidates only the iterator to the deleted |
| 705 | // element. |
| 706 | Views::iterator pe = p; |
| 707 | ++p; |
| 708 | this->views_.erase(pe); |
| 709 | } |
| 710 | else |
| 711 | { |
| 712 | gold_assert(!destroying); |
| 713 | p->second->clear_accessed(); |
| 714 | ++p; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | Saved_views::iterator q = this->saved_views_.begin(); |
| 719 | while (q != this->saved_views_.end()) |
| 720 | { |
| 721 | if (!(*q)->is_locked()) |
| 722 | { |
| 723 | delete *q; |
| 724 | q = this->saved_views_.erase(q); |
| 725 | } |
| 726 | else |
| 727 | { |
| 728 | gold_assert(!destroying); |
| 729 | ++q; |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | // Print statistical information to stderr. This is used for --stats. |
| 735 | |
| 736 | void |
| 737 | File_read::print_stats() |
| 738 | { |
| 739 | fprintf(stderr, _("%s: total bytes mapped for read: %llu\n"), |
| 740 | program_name, File_read::total_mapped_bytes); |
| 741 | fprintf(stderr, _("%s: maximum bytes mapped for read at one time: %llu\n"), |
| 742 | program_name, File_read::maximum_mapped_bytes); |
| 743 | } |
| 744 | |
| 745 | // Class File_view. |
| 746 | |
| 747 | File_view::~File_view() |
| 748 | { |
| 749 | gold_assert(this->file_.is_locked()); |
| 750 | this->view_->unlock(); |
| 751 | } |
| 752 | |
| 753 | // Class Input_file. |
| 754 | |
| 755 | // Create a file for testing. |
| 756 | |
| 757 | Input_file::Input_file(const Task* task, const char* name, |
| 758 | const unsigned char* contents, off_t size) |
| 759 | : file_() |
| 760 | { |
| 761 | this->input_argument_ = |
| 762 | new Input_file_argument(name, Input_file_argument::INPUT_FILE_TYPE_FILE, |
| 763 | "", false, Position_dependent_options()); |
| 764 | bool ok = this->file_.open(task, name, contents, size); |
| 765 | gold_assert(ok); |
| 766 | } |
| 767 | |
| 768 | // Return the position dependent options in force for this file. |
| 769 | |
| 770 | const Position_dependent_options& |
| 771 | Input_file::options() const |
| 772 | { |
| 773 | return this->input_argument_->options(); |
| 774 | } |
| 775 | |
| 776 | // Return the name given by the user. For -lc this will return "c". |
| 777 | |
| 778 | const char* |
| 779 | Input_file::name() const |
| 780 | { |
| 781 | return this->input_argument_->name(); |
| 782 | } |
| 783 | |
| 784 | // Return whether this file is in a system directory. |
| 785 | |
| 786 | bool |
| 787 | Input_file::is_in_system_directory() const |
| 788 | { |
| 789 | if (this->is_in_sysroot()) |
| 790 | return true; |
| 791 | return parameters->options().is_in_system_directory(this->filename()); |
| 792 | } |
| 793 | |
| 794 | // Return whether we are only reading symbols. |
| 795 | |
| 796 | bool |
| 797 | Input_file::just_symbols() const |
| 798 | { |
| 799 | return this->input_argument_->just_symbols(); |
| 800 | } |
| 801 | |
| 802 | // Return whether this is a file that we will search for in the list |
| 803 | // of directories. |
| 804 | |
| 805 | bool |
| 806 | Input_file::will_search_for() const |
| 807 | { |
| 808 | return (!IS_ABSOLUTE_PATH(this->input_argument_->name()) |
| 809 | && (this->input_argument_->is_lib() |
| 810 | || this->input_argument_->is_searched_file() |
| 811 | || this->input_argument_->extra_search_path() != NULL)); |
| 812 | } |
| 813 | |
| 814 | // Return the file last modification time. Calls gold_fatal if the stat |
| 815 | // system call failed. |
| 816 | |
| 817 | Timespec |
| 818 | File_read::get_mtime() |
| 819 | { |
| 820 | struct stat file_stat; |
| 821 | this->reopen_descriptor(); |
| 822 | |
| 823 | if (fstat(this->descriptor_, &file_stat) < 0) |
| 824 | gold_fatal(_("%s: stat failed: %s"), this->name_.c_str(), |
| 825 | strerror(errno)); |
| 826 | #ifdef HAVE_STAT_ST_MTIM |
| 827 | return Timespec(file_stat.st_mtim.tv_sec, file_stat.st_mtim.tv_nsec); |
| 828 | #else |
| 829 | return Timespec(file_stat.st_mtime, 0); |
| 830 | #endif |
| 831 | } |
| 832 | |
| 833 | // Open the file. |
| 834 | |
| 835 | // If the filename is not absolute, we assume it is in the current |
| 836 | // directory *except* when: |
| 837 | // A) input_argument_->is_lib() is true; |
| 838 | // B) input_argument_->is_searched_file() is true; or |
| 839 | // C) input_argument_->extra_search_path() is not empty. |
| 840 | // In each, we look in extra_search_path + library_path to find |
| 841 | // the file location, rather than the current directory. |
| 842 | |
| 843 | bool |
| 844 | Input_file::open(const Dirsearch& dirpath, const Task* task, int *pindex) |
| 845 | { |
| 846 | std::string name; |
| 847 | |
| 848 | // Case 1: name is an absolute file, just try to open it |
| 849 | // Case 2: name is relative but is_lib is false, is_searched_file is false, |
| 850 | // and extra_search_path is empty |
| 851 | if (IS_ABSOLUTE_PATH(this->input_argument_->name()) |
| 852 | || (!this->input_argument_->is_lib() |
| 853 | && !this->input_argument_->is_searched_file() |
| 854 | && this->input_argument_->extra_search_path() == NULL)) |
| 855 | { |
| 856 | name = this->input_argument_->name(); |
| 857 | this->found_name_ = name; |
| 858 | } |
| 859 | // Case 3: is_lib is true or is_searched_file is true |
| 860 | else if (this->input_argument_->is_lib() |
| 861 | || this->input_argument_->is_searched_file()) |
| 862 | { |
| 863 | // We don't yet support extra_search_path with -l. |
| 864 | gold_assert(this->input_argument_->extra_search_path() == NULL); |
| 865 | std::string n1, n2; |
| 866 | if (this->input_argument_->is_lib()) |
| 867 | { |
| 868 | n1 = "lib"; |
| 869 | n1 += this->input_argument_->name(); |
| 870 | if (parameters->options().is_static() |
| 871 | || !this->input_argument_->options().Bdynamic()) |
| 872 | n1 += ".a"; |
| 873 | else |
| 874 | { |
| 875 | n2 = n1 + ".a"; |
| 876 | n1 += ".so"; |
| 877 | } |
| 878 | } |
| 879 | else |
| 880 | n1 = this->input_argument_->name(); |
| 881 | name = dirpath.find(n1, n2, &this->is_in_sysroot_, pindex); |
| 882 | if (name.empty()) |
| 883 | { |
| 884 | gold_error(_("cannot find %s%s"), |
| 885 | this->input_argument_->is_lib() ? "-l" : "", |
| 886 | this->input_argument_->name()); |
| 887 | return false; |
| 888 | } |
| 889 | if (n2.empty() || name[name.length() - 1] == 'o') |
| 890 | this->found_name_ = n1; |
| 891 | else |
| 892 | this->found_name_ = n2; |
| 893 | } |
| 894 | // Case 4: extra_search_path is not empty |
| 895 | else |
| 896 | { |
| 897 | gold_assert(this->input_argument_->extra_search_path() != NULL); |
| 898 | |
| 899 | // First, check extra_search_path. |
| 900 | name = this->input_argument_->extra_search_path(); |
| 901 | if (!IS_DIR_SEPARATOR (name[name.length() - 1])) |
| 902 | name += '/'; |
| 903 | name += this->input_argument_->name(); |
| 904 | struct stat dummy_stat; |
| 905 | if (*pindex > 0 || ::stat(name.c_str(), &dummy_stat) < 0) |
| 906 | { |
| 907 | // extra_search_path failed, so check the normal search-path. |
| 908 | int index = *pindex; |
| 909 | if (index > 0) |
| 910 | --index; |
| 911 | name = dirpath.find(this->input_argument_->name(), "", |
| 912 | &this->is_in_sysroot_, &index); |
| 913 | if (name.empty()) |
| 914 | { |
| 915 | gold_error(_("cannot find %s"), |
| 916 | this->input_argument_->name()); |
| 917 | return false; |
| 918 | } |
| 919 | *pindex = index + 1; |
| 920 | } |
| 921 | this->found_name_ = this->input_argument_->name(); |
| 922 | } |
| 923 | |
| 924 | // Now that we've figured out where the file lives, try to open it. |
| 925 | |
| 926 | General_options::Object_format format = |
| 927 | this->input_argument_->options().format_enum(); |
| 928 | bool ok; |
| 929 | if (format == General_options::OBJECT_FORMAT_ELF) |
| 930 | ok = this->file_.open(task, name); |
| 931 | else |
| 932 | { |
| 933 | gold_assert(format == General_options::OBJECT_FORMAT_BINARY); |
| 934 | ok = this->open_binary(task, name); |
| 935 | } |
| 936 | |
| 937 | if (!ok) |
| 938 | { |
| 939 | gold_error(_("cannot open %s: %s"), |
| 940 | name.c_str(), strerror(errno)); |
| 941 | return false; |
| 942 | } |
| 943 | |
| 944 | return true; |
| 945 | } |
| 946 | |
| 947 | // Open a file for --format binary. |
| 948 | |
| 949 | bool |
| 950 | Input_file::open_binary(const Task* task, const std::string& name) |
| 951 | { |
| 952 | // In order to open a binary file, we need machine code, size, and |
| 953 | // endianness. We may not have a valid target at this point, in |
| 954 | // which case we use the default target. |
| 955 | parameters_force_valid_target(); |
| 956 | const Target& target(parameters->target()); |
| 957 | |
| 958 | Binary_to_elf binary_to_elf(target.machine_code(), |
| 959 | target.get_size(), |
| 960 | target.is_big_endian(), |
| 961 | name); |
| 962 | if (!binary_to_elf.convert(task)) |
| 963 | return false; |
| 964 | return this->file_.open(task, name, binary_to_elf.converted_data_leak(), |
| 965 | binary_to_elf.converted_size()); |
| 966 | } |
| 967 | |
| 968 | } // End namespace gold. |