1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
25 #ifndef __OPAQUE_SECTION__
26 #define __OPAQUE_SECTION__
31 #include "ObjectFile.h"
33 namespace opaque_section {
36 class Segment : public ObjectFile::Segment
39 Segment(const char* name) { fName = name; }
40 virtual const char* getName() const { return fName; }
41 virtual bool isContentReadable() const { return true; }
42 virtual bool isContentWritable() const { return false; }
43 virtual bool isContentExecutable() const { return (strcmp(fName, "__TEXT") == 0); }
49 class Reader : public ObjectFile::Reader
52 Reader(const char* segmentName, const char* sectionName, const char* path,
53 const uint8_t fileContent[], uint64_t fileLength, uint32_t ordinal, const char* symbolName=NULL);
56 void addSectionReference(uint8_t kind, uint64_t offsetInSection, const ObjectFile::Atom* targetAtom,
57 uint64_t offsetInTarget, const ObjectFile::Atom* fromTargetAtom=NULL, uint64_t offsetInFromTarget=0);
59 virtual const char* getPath() { return fPath; }
60 virtual time_t getModificationTime() { return 0; }
61 virtual DebugInfoKind getDebugInfoKind() { return ObjectFile::Reader::kDebugInfoNone; }
62 virtual std::vector<class ObjectFile::Atom*>& getAtoms() { return fAtoms; }
63 virtual std::vector<class ObjectFile::Atom*>* getJustInTimeAtomsFor(const char* name) { return NULL; }
64 virtual std::vector<Stab>* getStabs() { return NULL; }
68 std::vector<class ObjectFile::Atom*> fAtoms;
71 class Reference : public ObjectFile::Reference
74 Reference(uint8_t kind, uint64_t fixupOffset, const ObjectFile::Atom* target, uint64_t targetOffset,
75 const ObjectFile::Atom* fromTarget=NULL, uint64_t fromTargetOffset=0)
76 : fFixUpOffset(fixupOffset), fTarget(target), fTargetOffset(targetOffset), fKind(kind),
77 fFromTarget(fromTarget), fFromTargetOffset(fromTargetOffset) {}
78 virtual ~Reference() {}
81 virtual ObjectFile::Reference::TargetBinding getTargetBinding() const { return ObjectFile::Reference::kBoundDirectly; }
82 virtual ObjectFile::Reference::TargetBinding getFromTargetBinding() const{ return ObjectFile::Reference::kDontBind; }
83 virtual uint8_t getKind() const { return fKind; }
84 virtual uint64_t getFixUpOffset() const { return fFixUpOffset; }
85 virtual const char* getTargetName() const { return fTarget->getName(); }
86 virtual ObjectFile::Atom& getTarget() const { return *((ObjectFile::Atom*)fTarget); }
87 virtual uint64_t getTargetOffset() const { return fTargetOffset; }
88 virtual ObjectFile::Atom& getFromTarget() const { return *((ObjectFile::Atom*)fFromTarget); }
89 virtual const char* getFromTargetName() const { return fFromTarget->getName(); }
90 virtual uint64_t getFromTargetOffset() const { return fFromTargetOffset; }
91 virtual void setTarget(ObjectFile::Atom&, uint64_t offset) { throw "can't set target"; }
92 virtual void setFromTarget(ObjectFile::Atom&) { throw "can't set from target"; }
93 virtual const char* getDescription() const { return "opaque section reference"; }
96 uint64_t fFixUpOffset;
97 const ObjectFile::Atom* fTarget;
98 uint64_t fTargetOffset;
100 const ObjectFile::Atom* fFromTarget;
101 uint64_t fFromTargetOffset;
105 class Atom : public ObjectFile::Atom {
107 virtual ObjectFile::Reader* getFile() const { return &fOwner; }
108 virtual bool getTranslationUnitSource(const char** dir, const char** name) const { return false; }
109 virtual const char* getName() const { return fName; }
110 virtual const char* getDisplayName() const;
111 virtual Scope getScope() const { return ObjectFile::Atom::scopeLinkageUnit; }
112 virtual DefinitionKind getDefinitionKind() const { return kRegularDefinition; }
113 virtual SymbolTableInclusion getSymbolTableInclusion() const { return ObjectFile::Atom::kSymbolTableNotIn; }
114 virtual bool dontDeadStrip() const { return true; }
115 virtual bool isZeroFill() const { return false; }
116 virtual bool isThumb() const { return false; }
117 virtual uint64_t getSize() const { return fFileLength; }
118 virtual std::vector<ObjectFile::Reference*>& getReferences() const { return (std::vector<ObjectFile::Reference*>&)(fReferences); }
119 virtual bool mustRemainInSection() const { return false; }
120 virtual const char* getSectionName() const { return fSectionName; }
121 virtual Segment& getSegment() const { return fSegment; }
122 virtual ObjectFile::Atom& getFollowOnAtom() const { return *((ObjectFile::Atom*)NULL); }
123 virtual uint32_t getOrdinal() const { return fOrdinal; }
124 virtual std::vector<ObjectFile::LineInfo>* getLineInfo() const { return NULL; }
125 virtual ObjectFile::Alignment getAlignment() const { return ObjectFile::Alignment(4); }
126 virtual void copyRawContent(uint8_t buffer[]) const;
128 virtual void setScope(Scope) { }
133 Atom(Reader& owner, Segment& segment, const char* sectionName,
134 const uint8_t fileContent[], uint64_t fileLength, uint32_t ordinal, const char* symbolName);
140 const char* fSectionName;
141 const uint8_t* fFileContent;
143 uint64_t fFileLength;
144 std::vector<ObjectFile::Reference*> fReferences;
149 Atom::Atom(Reader& owner, Segment& segment, const char* sectionName, const uint8_t fileContent[], uint64_t fileLength, uint32_t ordinal, const char* symbolName)
150 : fOwner(owner), fSegment(segment), fSectionName(sectionName), fFileContent(fileContent), fOrdinal(ordinal), fFileLength(fileLength)
152 if ( symbolName != NULL )
153 fName = strdup(symbolName);
155 asprintf((char**)&fName, "__section$%s%s", segment.getName(), sectionName);
159 Reader::Reader(const char* segmentName, const char* sectionName, const char* path, const uint8_t fileContent[],
160 uint64_t fileLength, uint32_t ordinal, const char* symbolName)
163 fAtoms.push_back(new Atom(*this, *(new Segment(segmentName)), strdup(sectionName), fileContent, fileLength, ordinal, symbolName));
170 void Reader::addSectionReference(uint8_t kind, uint64_t offsetInSection, const ObjectFile::Atom* targetAtom,
171 uint64_t offsetInTarget, const ObjectFile::Atom* fromTargetAtom, uint64_t offsetInFromTarget)
173 fAtoms[0]->getReferences().push_back(new Reference(kind, offsetInSection, targetAtom, offsetInTarget, fromTargetAtom, offsetInFromTarget));
177 const char* Atom::getDisplayName() const
179 static char name[64];
180 sprintf(name, "opaque section %s %s", fSegment.getName(), fSectionName);
185 void Atom::copyRawContent(uint8_t buffer[]) const
187 memcpy(buffer, fFileContent, fFileLength);
196 #endif // __OPAQUE_SECTION__