Merge pull request #8 from biergaizi/upstream
[darwin-xtools.git] / ld64 / src / ld / parsers / opaque_section_file.cpp
blob081e957a3b8f1e8cf9437d689e3e7fcbd71b8bc5
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2005-2009 Apple Inc. All rights reserved.
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.
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@
26 #include <vector>
27 #include <map>
29 #include "ld.hpp"
30 #include "opaque_section_file.h"
33 namespace opaque_section {
37 class Atom : public ld::Atom {
38 public:
39 virtual ld::File* file() const { return (ld::File*)&_file; }
40 virtual const char* name() const { return _name; }
41 virtual uint64_t size() const { return _size; }
42 virtual uint64_t objectAddress() const { return 0; }
43 virtual void copyRawContent(uint8_t buffer[]) const
44 { memcpy(buffer, _content, _size); }
45 virtual void setScope(Scope) { }
47 protected:
48 friend class File;
49 Atom(class File& f, const char* n, const uint8_t* content, uint64_t sz);
50 virtual ~Atom() {}
52 class File& _file;
53 const char* _name;
54 const uint8_t* _content;
55 uint64_t _size;
59 class File : public ld::File
61 public:
62 File(const char* segmentName, const char* sectionName, const char* pth,
63 const uint8_t fileContent[], uint64_t fileLength,
64 const char* symbolName="sect_create")
65 : ld::File(pth, 0, ld::File::Ordinal::NullOrdinal(), Other),
66 _atom(*this, symbolName, fileContent, fileLength),
67 _section(segmentName, sectionName, ld::Section::typeSectCreate) { }
68 virtual ~File() { }
70 virtual bool forEachAtom(ld::File::AtomHandler& h) const { h.doAtom(_atom); return true; }
71 virtual bool justInTimeforEachAtom(const char* name, ld::File::AtomHandler&) const { return false; }
73 ld::Atom* atom() { return &_atom; }
74 private:
75 friend class Atom;
77 Atom _atom;
78 ld::Section _section;
81 Atom::Atom(File& f, const char* n, const uint8_t* content, uint64_t sz)
82 : ld::Atom(f._section, ld::Atom::definitionRegular, ld::Atom::combineNever,
83 ld::Atom::scopeTranslationUnit, ld::Atom::typeUnclassified,
84 symbolTableNotIn, true, false, false, ld::Atom::Alignment(0)),
85 _file(f), _name(n), _content(content), _size(sz) {}
89 // main function used by linker for -sectcreate
91 ld::File* parse(const char* segmentName, const char* sectionName, const char* path,
92 const uint8_t fileContent[], uint64_t fileLength,
93 const char* symbolName)
95 return new File(segmentName, sectionName, path, fileContent, fileLength, symbolName);
99 } // namespace opaque_section