]> git.saurik.com Git - apple/ld64.git/blob - src/SectCreate.cpp
a77a78746b179be87e60808f9b3740b2a2d4bcf6
[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 dontDeadStrip() const { return true; }
74 virtual bool isZeroFill() const { return false; }
75 virtual uint64_t getSize() const { return fFileLength; }
76 virtual std::vector<ObjectFile::Reference*>& getReferences() const { return fgEmptyReferenceList; }
77 virtual bool mustRemainInSection() const { return false; }
78 virtual const char* getSectionName() const { return fSectionName; }
79 virtual Segment& getSegment() const { return fSegment; }
80 virtual bool requiresFollowOnAtom() const{ return false; }
81 virtual ObjectFile::Atom& getFollowOnAtom() const { return *((ObjectFile::Atom*)NULL); }
82 virtual std::vector<ObjectFile::LineInfo>* getLineInfo() const { return NULL; }
83 virtual uint8_t getAlignment() const { return 4; }
84 virtual void copyRawContent(uint8_t buffer[]) const;
85
86 virtual void setScope(Scope) { }
87
88 protected:
89 friend class Reader;
90
91 Atom(Reader& owner, Segment& segment, const char* sectionName, const uint8_t fileContent[], uint64_t fileLength)
92 : fOwner(owner), fSegment(segment), fSectionName(sectionName), fFileContent(fileContent), fFileLength(fileLength) { }
93 virtual ~Atom() {}
94
95 Reader& fOwner;
96 Segment& fSegment;
97 const char* fSectionName;
98 const uint8_t* fFileContent;
99 uint64_t fFileLength;
100
101 static std::vector<ObjectFile::Reference*> fgEmptyReferenceList;
102 };
103
104
105 std::vector<ObjectFile::Reference*> Atom::fgEmptyReferenceList;
106
107
108
109 Reader::Reader(const char* segmentName, const char* sectionName, const char* path, const uint8_t fileContent[], uint64_t fileLength)
110 : fPath(path)
111 {
112 fAtoms.push_back(new Atom(*this, *(new Segment(segmentName)), sectionName, fileContent, fileLength));
113 }
114
115 Reader::~Reader()
116 {
117 }
118
119
120 const char* Atom::getDisplayName() const
121 {
122 static char name[64];
123 sprintf(name, "-sectcreate %s %s", fSegment.getName(), fSectionName);
124 return name;
125 }
126
127
128 void Atom::copyRawContent(uint8_t buffer[]) const
129 {
130 memcpy(buffer, fFileContent, fFileLength);
131 }
132
133 Reader* MakeReader(const char* segmentName, const char* sectionName, const char* path, const uint8_t fileContent[], uint64_t fileLength)
134 {
135 return new Reader(segmentName, sectionName, path, fileContent, fileLength);
136 }
137
138
139
140 };
141
142
143
144
145
146
147