]> git.saurik.com Git - apple/ld64.git/blame - src/ld/passes/huge.cpp
ld64-264.3.101.tar.gz
[apple/ld64.git] / src / ld / passes / huge.cpp
CommitLineData
a645023d
A
1/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2009 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25
26#include <stdint.h>
27#include <math.h>
28#include <unistd.h>
29#include <dlfcn.h>
30#include <mach/machine.h>
31
32#include <vector>
eaf282aa 33#include <map>
a645023d
A
34
35#include "ld.hpp"
36#include "huge.h"
37
38namespace ld {
39namespace passes {
40namespace huge {
41
42class NullAtom
43{
44public:
45 bool operator()(const ld::Atom* atom) const {
46 return (atom == NULL);
47 }
48};
49
50void doPass(const Options& opts, ld::Internal& state)
51{
52 const bool log = false;
53
54 // only make make __huge section in final linked images
55 if ( opts.outputKind() == Options::kObjectFile )
56 return;
57
58 // only make make __huge section for x86_64
59 if ( opts.architecture() != CPU_TYPE_X86_64 )
60 return;
61
62 // only needed if some (non-linkedit) atoms have an addresss >2GB from base address
63 state.usingHugeSections = false;
64 uint64_t address = 0;
65 for (std::vector<ld::Internal::FinalSection*>::iterator sit=state.sections.begin(); sit != state.sections.end(); ++sit) {
66 ld::Internal::FinalSection* sect = *sit;
67 if ( sect->type() == ld::Section::typePageZero )
68 continue;
69 if ( sect->type() == ld::Section::typeStack )
70 continue;
71 for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
72 const ld::Atom* atom = *ait;
73 if ( (address > 0x7FFFFFFFLL) && !sect->isSectionHidden() ) {
74 state.usingHugeSections = true;
75 if (log) fprintf(stderr, "atom: %s is >2GB (0x%09llX), so enabling huge mode\n", atom->name(), address);
76 break;
77 }
78 address += atom->size();
79 }
80 if ( state.usingHugeSections )
81 break;
82 }
83 if ( !state.usingHugeSections )
84 return;
85
86 // move all zero fill atoms that >1MB in size to a new __huge section
d425e388 87 ld::Internal::FinalSection* hugeSection = state.getFinalSection(*new ld::Section("__DATA", "__huge", ld::Section::typeZeroFill));
a645023d
A
88 for (std::vector<ld::Internal::FinalSection*>::iterator sit=state.sections.begin(); sit != state.sections.end(); ++sit) {
89 ld::Internal::FinalSection* sect = *sit;
90 if ( sect == hugeSection )
91 continue;
92 if ( sect->type() == ld::Section::typeZeroFill ) {
93 bool movedSome = false;
94 for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
95 const ld::Atom* atom = *ait;
96 if ( atom->size() > 1024*1024 ) {
97 hugeSection->atoms.push_back(atom);
eaf282aa 98 state.atomToSection[atom] = hugeSection;
a645023d
A
99 if (log) fprintf(stderr, "moved to __huge: %s, size=%llu\n", atom->name(), atom->size());
100 *ait = NULL; // change atom to NULL for later bulk removal
101 movedSome = true;
102 }
103 }
104 if ( movedSome )
105 sect->atoms.erase(std::remove_if(sect->atoms.begin(), sect->atoms.end(), NullAtom()), sect->atoms.end());
106 }
107 }
108
109
110}
111
112
113} // namespace huge
114} // namespace passes
115} // namespace ld