1/*
2    Title:     Export memory as a Mach object file
3    Author:    David C. J. Matthews.
4
5    Copyright (c) 2006,2016 David C. J. Matthews
6
7
8    This library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public
10    License version 2.1 as published by the Free Software Foundation.
11
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR H PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with this library; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20*/
21
22#ifndef MachExport_H_INCLUDED
23#define MachExport_H_INCLUDED
24
25#include "config.h"
26
27#include "scanaddrs.h" // For base class
28#include "exporter.h"
29#include <mach-o/reloc.h>
30
31
32class MachoExport: public Exporter, public ScanAddress
33{
34public:
35    MachoExport(): relocationCount(0), symbolNum(0) {}
36public:
37    virtual void exportStore(void);
38
39private:
40    // ScanAddress overrides
41    virtual void ScanConstant(PolyObject *base, byte *addrOfConst, ScanRelocationKind code);
42    // At the moment we should only get calls to ScanConstant.
43    virtual PolyObject *ScanObjectAddress(PolyObject *base) { return base; }
44    virtual void addExternalReference(void *addr, const char *name, bool isFuncPtr);
45
46private:
47    void setRelocationAddress(void *p, int32_t *reloc);
48    PolyWord createRelocation(PolyWord p, void *relocAddr);
49    PolyWord writeRelocation(POLYUNSIGNED offset, void *relocAddr, unsigned symbolNumber, bool isExtern);
50    void alignFile(int align);
51    void createStructsRelocation(unsigned area, POLYUNSIGNED offset);
52    void adjustOffset(unsigned area, POLYUNSIGNED &offset);
53
54    unsigned relocationCount;
55    ExportStringTable stringTable;
56
57    // Table and count for external references.
58    ExportStringTable externTable;
59    unsigned symbolNum;
60};
61
62#endif
63