]> git.saurik.com Git - apple/ld64.git/blob - src/SectCreate.cpp
7296c1a96e16df0d9a0361d9a0cc5cbeaa715ac6
[apple/ld64.git] / src / SectCreate.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2005-2006 Apple Computer, 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 #include <vector>
26
27 #include "ObjectFile.h"
28
29 namespace SectCreate {
30
31
32 class Segment : public ObjectFile::Segment
33 {
34 public:
35 Segment(const char* name) { fName = name; }
36 virtual const char* getName() const { return fName; }
37 virtual bool isContentReadable() const { return true; }
38 virtual bool isContentWritable() const { return false; }
39 virtual bool isContentExecutable() const { return false; }
40 private:
41 const char* fName;
42 };
43
44
45 class Reader : public ObjectFile::Reader
46 {
47 public:
48 Reader(const char* segmentName, const char* sectionName, const char* path, const uint8_t fileContent[], uint64_t fileLength);
49 virtual ~Reader();
50
51 virtual const char* getPath() { return fPath; }
52 virtual time_t getModificationTime() { return 0; }
53 virtual DebugInfoKind getDebugInfoKind() { return ObjectFile::Reader::kDebugInfoNone; }
54 virtual std::vector<class ObjectFile::Atom*>& getAtoms() { return fAtoms; }
55 virtual std::vector<class ObjectFile::Atom*>* getJustInTimeAtomsFor(const char* name) { return NULL; }
56 virtual std::vector<Stab>* getStabs() { return NULL; }
57
58 private:
59 const char* fPath;
60 std::vector<class ObjectFile::Atom*> fAtoms;
61 };
62
63
64 class Atom : public ObjectFile::Atom {
65 public:
66 virtual ObjectFile::Reader* getFile() const { return &fOwner; }
67 virtual bool getTranslationUnitSource(const char** dir, const char** name) const { return false; }
68 virtual const char* getName() const { return NULL; }
69 virtual const char* getDisplayName() const;
70 virtual Scope getScope() const { return ObjectFile::Atom::scopeTranslationUnit; }
71 virtual DefinitionKind getDefinitionKind() const { return kRegularDefinition; }
72 virtual SymbolTableInclusion getSymbolTableInclusion() const { return ObjectFile::Atom::kSymbolTableNotIn; }
73 virtual bool isZeroFill() const { return false; }
74 virtual uint64_t getSize() const { return fFileLength; }
75 virtual std::vector<ObjectFile::Reference*>& getReferences() const { return fgEmptyReferenceList; }
76 virtual bool mustRemainInSection() const { return false; }
77 virtual const char* getSectionName() const { return fSectionName; }
78 virtual Segment& getSegment() const { return fSegment; }
79 virtual bool requiresFollowOnAtom() const{ return false; }
80 virtual ObjectFile::Atom& getFollowOnAtom() const { return *((ObjectFile::Atom*)NULL); }
81 virtual std::vector<ObjectFile::LineInfo>* getLineInfo() const { return NULL; }
82 virtual uint8_t getAlignment() const { return 4; }
83 virtual void copyRawContent(uint8_t buffer[]) const;
84
85 virtual void setScope(Scope) { }
86
87 protected:
88 friend class Reader;
89
90 Atom(Reader& owner, Segment& segment, const char* sectionName, const uint8_t fileContent[], uint64_t fileLength)
91 : fOwner(owner), fSegment(segment), fSectionName(sectionName), fFileContent(fileContent), fFileLength(fileLength) { setDontDeadStrip(); }
92 virtual ~Atom() {}
93
94 Reader& fOwner;
95 Segment& fSegment;
96 const char* fSectionName;
97 const uint8_t* fFileContent;
98 uint64_t fFileLength;
99
100 static std::vector<ObjectFile::Reference*> fgEmptyReferenceList;
101 };
102
103
104 std::vector<ObjectFile::Reference*> Atom::fgEmptyReferenceList;
105
106
107
108 Reader::Reader(const char* segmentName, const char* sectionName, const char* path, const uint8_t fileContent[], uint64_t fileLength)
109 : fPath(path)
110 {
111 fAtoms.push_back(new Atom(*this, *(new Segment(segmentName)), sectionName, fileContent, fileLength));
112 }
113
114 Reader::~Reader()
115 {
116 }
117
118
119 const char* Atom::getDisplayName() const
120 {
121 static char name[64];
122 sprintf(name, "-sectcreate %s %s", fSegment.getName(), fSectionName);
123 return name;
124 }
125
126
127 void Atom::copyRawContent(uint8_t buffer[]) const
128 {
129 memcpy(buffer, fFileContent, fFileLength);
130 }
131
132 Reader* MakeReader(const char* segmentName, const char* sectionName, const char* path, const uint8_t fileContent[], uint64_t fileLength)
133 {
134 return new Reader(segmentName, sectionName, path, fileContent, fileLength);
135 }
136
137
138
139 };
140
141
142
143
144
145
146