]> git.saurik.com Git - apple/ld64.git/blame - src/SectCreate.cpp
ld64-59.3.tar.gz
[apple/ld64.git] / src / SectCreate.cpp
CommitLineData
d696c285
A
1/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved.
c2646906
A
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
29namespace SectCreate {
30
31
32class Segment : public ObjectFile::Segment
33{
34public:
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; }
40private:
41 const char* fName;
42};
43
44
45class Reader : public ObjectFile::Reader
46{
47public:
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; }
d696c285
A
52 virtual time_t getModificationTime() { return 0; }
53 virtual DebugInfoKind getDebugInfoKind() { return ObjectFile::Reader::kDebugInfoNone; }
c2646906
A
54 virtual std::vector<class ObjectFile::Atom*>& getAtoms() { return fAtoms; }
55 virtual std::vector<class ObjectFile::Atom*>* getJustInTimeAtomsFor(const char* name) { return NULL; }
d696c285 56 virtual std::vector<Stab>* getStabs() { return NULL; }
c2646906
A
57
58private:
59 const char* fPath;
60 std::vector<class ObjectFile::Atom*> fAtoms;
61};
62
63
64class Atom : public ObjectFile::Atom {
65public:
66 virtual ObjectFile::Reader* getFile() const { return &fOwner; }
d696c285 67 virtual bool getTranslationUnitSource(const char** dir, const char** name) const { return false; }
c2646906
A
68 virtual const char* getName() const { return NULL; }
69 virtual const char* getDisplayName() const;
70 virtual Scope getScope() const { return ObjectFile::Atom::scopeTranslationUnit; }
d696c285
A
71 virtual DefinitionKind getDefinitionKind() const { return kRegularDefinition; }
72 virtual SymbolTableInclusion getSymbolTableInclusion() const { return ObjectFile::Atom::kSymbolTableNotIn; }
69a49097 73 virtual bool dontDeadStrip() const { return true; }
c2646906 74 virtual bool isZeroFill() const { return false; }
c2646906
A
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); }
d696c285 82 virtual std::vector<ObjectFile::LineInfo>* getLineInfo() const { return NULL; }
c2646906 83 virtual uint8_t getAlignment() const { return 4; }
c2646906 84 virtual void copyRawContent(uint8_t buffer[]) const;
c2646906
A
85
86 virtual void setScope(Scope) { }
c2646906
A
87
88protected:
89 friend class Reader;
90
91 Atom(Reader& owner, Segment& segment, const char* sectionName, const uint8_t fileContent[], uint64_t fileLength)
69a49097 92 : fOwner(owner), fSegment(segment), fSectionName(sectionName), fFileContent(fileContent), fFileLength(fileLength) { }
c2646906
A
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
105std::vector<ObjectFile::Reference*> Atom::fgEmptyReferenceList;
106
107
108
109Reader::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
115Reader::~Reader()
116{
117}
118
119
120const 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
128void Atom::copyRawContent(uint8_t buffer[]) const
129{
130 memcpy(buffer, fFileContent, fFileLength);
131}
132
c2646906
A
133Reader* 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