linux: fix build
[odcctools-svp.git] / ld64 / SectCreate.cpp
blob41a6ec20aff903cab911d29834fdfe5d6441b585
1 /*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <vector>
26 #include <string.h>
27 #include <stdio.h>
29 #include "ObjectFile.h"
31 namespace SectCreate {
34 class Segment : public ObjectFile::Segment
36 public:
37 Segment(const char* name) { fName = name; }
38 virtual const char* getName() const { return fName; }
39 virtual bool isContentReadable() const { return true; }
40 virtual bool isContentWritable() const { return false; }
41 virtual bool isContentExecutable() const { return false; }
42 private:
43 const char* fName;
47 class Reader : public ObjectFile::Reader
49 public:
50 Reader(const char* segmentName, const char* sectionName, const char* path, const uint8_t fileContent[], uint64_t fileLength);
51 virtual ~Reader();
53 virtual const char* getPath() { return fPath; }
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<ObjectFile::StabsInfo>* getStabsDebugInfo() { return NULL; }
58 private:
59 const char* fPath;
60 std::vector<class ObjectFile::Atom*> fAtoms;
64 class Atom : public ObjectFile::Atom {
65 public:
66 virtual ObjectFile::Reader* getFile() const { return &fOwner; }
67 virtual const char* getName() const { return NULL; }
68 virtual const char* getDisplayName() const;
69 virtual Scope getScope() const { return ObjectFile::Atom::scopeTranslationUnit; }
70 virtual bool isTentativeDefinition() const { return false; }
71 virtual bool isWeakDefinition() const { return false; }
72 virtual bool isCoalesableByName() const { return false; }
73 virtual bool isCoalesableByValue() const { return false; }
74 virtual bool isZeroFill() const { return false; }
75 virtual bool dontDeadStrip() const { return true; }
76 virtual bool dontStripName() const { return false; }
77 virtual bool isImportProxy() const { return false; }
78 virtual uint64_t getSize() const { return fFileLength; }
79 virtual std::vector<ObjectFile::Reference*>& getReferences() const { return fgEmptyReferenceList; }
80 virtual bool mustRemainInSection() const { return false; }
81 virtual const char* getSectionName() const { return fSectionName; }
82 virtual Segment& getSegment() const { return fSegment; }
83 virtual bool requiresFollowOnAtom() const{ return false; }
84 virtual ObjectFile::Atom& getFollowOnAtom() const { return *((ObjectFile::Atom*)NULL); }
85 virtual std::vector<ObjectFile::StabsInfo>* getStabsDebugInfo() const { return NULL; }
86 virtual uint8_t getAlignment() const { return 4; }
87 virtual WeakImportSetting getImportWeakness() const { return ObjectFile::Atom::kWeakUnset; }
88 virtual void copyRawContent(uint8_t buffer[]) const;
89 virtual void writeContent(bool finalLinkedImage, ObjectFile::ContentWriter&) const;
91 virtual void setScope(Scope) { }
92 virtual void setImportWeakness(bool) { }
94 protected:
95 friend class Reader;
97 Atom(Reader& owner, Segment& segment, const char* sectionName, const uint8_t fileContent[], uint64_t fileLength)
98 : fOwner(owner), fSegment(segment), fSectionName(sectionName), fFileContent(fileContent), fFileLength(fileLength) {}
99 virtual ~Atom() {}
101 Reader& fOwner;
102 Segment& fSegment;
103 const char* fSectionName;
104 const uint8_t* fFileContent;
105 uint64_t fFileLength;
107 static std::vector<ObjectFile::Reference*> fgEmptyReferenceList;
111 std::vector<ObjectFile::Reference*> Atom::fgEmptyReferenceList;
115 Reader::Reader(const char* segmentName, const char* sectionName, const char* path, const uint8_t fileContent[], uint64_t fileLength)
116 : fPath(path)
118 fAtoms.push_back(new Atom(*this, *(new Segment(segmentName)), sectionName, fileContent, fileLength));
121 Reader::~Reader()
126 const char* Atom::getDisplayName() const
128 static char name[64];
129 sprintf(name, "-sectcreate %s %s", fSegment.getName(), fSectionName);
130 return name;
134 void Atom::copyRawContent(uint8_t buffer[]) const
136 memcpy(buffer, fFileContent, fFileLength);
139 void Atom::writeContent(bool finalLinkedImage, ObjectFile::ContentWriter& writer) const
141 writer.write(0, fFileContent, fFileLength);
145 Reader* MakeReader(const char* segmentName, const char* sectionName, const char* path, const uint8_t fileContent[], uint64_t fileLength)
147 return new Reader(segmentName, sectionName, path, fileContent, fileLength);