summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-res/src/machosubwriter.inc
blob: bb1194f0abce055a2fad117ae6b70c8be4f50c48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2008 by Giulio Bernardi

    Resource writer for Mach-O files

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

type
  _TMachOSymbolTable_ = class(TMachOSymbolTable)
  protected
    function AddSymbol(aName : string; sect : byte; addr : longword;
      glob : boolean) : integer; override;
  protected
  public
    procedure WriteToStream(aStream : TStream); override;
  end;

  _TMachOSubWriter_ = class(TAbstractMachOSubWriter)
  private
    procedure SwapSection(var aSection: _TSection_);
  protected
    procedure PrescanResourceTree; override;
    procedure WriteResHeader(aStream : TStream; aResources : TResources); override;
    procedure WriteNodeInfos(aStream : TStream); override;
    procedure WriteNodeInfo(aStream : TStream; aNode : TResourceTreeNode); override;
    procedure AllocateSpaceForLoadCommands(aStream : TStream); override;

    procedure FixLoadCommands(aStream : TStream; aResources : TResources); override;
    procedure FixResHeader(aStream : TStream); override;
  public
    constructor Create(aParent : TMachOResourceWriter; const aMachineType
      : TMachOMachineType; const aSubMachineType: TMachoSubMachineType;
      const aOppositeEndianess : boolean); override;
  end;

{ _TMachOSymbolTable_ }

function _TMachOSymbolTable_.AddSymbol(aName: string; sect: byte; addr: longword;
  glob: boolean): integer;
var p : _PNlist_;
begin
  p:=GetMem(sizeof(_TNlist_));
  p^.strx:=fStringTable.Add(aName);
  p^._type:=N_SECT;
  if glob then p^._type:=p^._type or N_EXT;
  p^.desc:=0;
  p^.sect:=sect;
  p^.value:=addr;
  Result:=fList.Count;
  fList.Add(p);
end;

procedure _TMachOSymbolTable_.WriteToStream(aStream: TStream);
var nlist : _TNlist_;
    i : integer;
begin
  for i:=0 to fList.Count-1 do
  begin
    nlist:=_PNlist_(fList[i])^;
    if fOppositeEndianess then
    begin
      nlist.strx:=SwapEndian(nlist.strx);
      nlist.desc:=SwapEndian(nlist.desc);
      nlist.value:=SwapEndian(nlist.value);
    end;
    aStream.WriteBuffer(nlist,sizeof(nlist));
  end;
end;

{ _TMachOSubWriter_ }

procedure _TMachOSubWriter_.SwapSection(var aSection: _TSection_);
begin
  aSection.addr:=SwapEndian(aSection.addr);
  aSection.size:=SwapEndian(aSection.size);
  aSection.offset:=SwapEndian(aSection.offset);
  aSection.align:=SwapEndian(aSection.align);
  aSection.reloff:=SwapEndian(aSection.reloff);
  aSection.nreloc:=SwapEndian(aSection.nreloc);
  aSection.flags:=SwapEndian(aSection.flags);
  aSection.reserved1:=SwapEndian(aSection.reserved1);
  aSection.reserved2:=SwapEndian(aSection.reserved2);
end;

procedure _TMachOSubWriter_.PrescanResourceTree;
begin
  fResStrTable.Clear;
  fRoot.SubDirRVA:=sizeof(_TResHdr_)+sizeof(_TResInfoNode_);
  fResStrTable.StartOfs:=PrescanNode(fRoot,sizeof(_TResInfoNode_));
  if fResStrTable.Used then
    fDataCurOfs:=NextAligned(fDataAlignment,fResStrTable.StartOfs+fResStrTable.Size)
  else
    fDataCurOfs:=fResStrTable.StartOfs;
end;

procedure _TMachOSubWriter_.WriteResHeader(aStream: TStream;
  aResources: TResources);
var hdr : _TResHdr_;
begin
  hdr.rootptr:=sizeof(hdr);
  hdr.count:=aResources.Count;
  hdr.usedhandles:=0;
  hdr.handles:=0;
  fRelocations.Add(0,1);
  fRelocations.Add(sizeof(hdr.rootptr)+sizeof(hdr.count)+sizeof(hdr.usedhandles),2);
  if fOppositeEndianess then
  begin
    hdr.rootptr:=SwapEndian(hdr.rootptr);
    hdr.count:=SwapEndian(hdr.count);
    //handles must be fixed later
//    hdr.usedhandles:=SwapEndian(hdr.usedhandles);
//    hdr.handles:=SwapEndian(hdr.handles);
  end;
  aStream.WriteBuffer(hdr,sizeof(hdr));
end;

procedure _TMachOSubWriter_.WriteNodeInfos(aStream: TStream);
begin
  fCurOfs:=sizeof(_TResHdr_);
  WriteNodeInfo(aStream,fRoot);
  WriteSubNodes(aStream,fRoot);
end;

procedure _TMachOSubWriter_.WriteNodeInfo(aStream: TStream;
  aNode: TResourceTreeNode);
var infonode : _TResInfoNode_;
begin
  if aNode.Desc.DescType=dtID then
    infonode.nameid:=aNode.Desc.ID
  else
  begin
    infonode.nameid:=fResStrTable.StartOfs+aNode.NameRVA;
    fRelocations.Add(fCurOfs,1);
  end;
  infonode.ncount:=aNode.NamedCount;
  if aNode.IsLeaf then
  begin
    infonode.idcountsize:=aNode.Data.RawData.Size;
    infonode.subptr:=fDataCurOfs;
    fDataCurOfs:=NextAligned(fDataAlignment,fDataCurOfs+infonode.idcountsize);
  end
  else
  begin
    infonode.idcountsize:=aNode.IDCount;
    infonode.subptr:=aNode.SubDirRVA;
  end;
  fRelocations.Add(
    fCurOfs+sizeof(infonode.nameid)+sizeof(infonode.ncount)+
    sizeof(infonode.idcountsize),1);
  if fOppositeEndianess then
  begin
    infonode.nameid:=SwapEndian(infonode.nameid);
    infonode.ncount:=SwapEndian(infonode.ncount);
    infonode.idcountsize:=SwapEndian(infonode.idcountsize);
    infonode.subptr:=SwapEndian(infonode.subptr);
  end;
  aStream.WriteBuffer(infonode,sizeof(infonode));
  inc(fCurOfs,sizeof(infonode));
end;

procedure _TMachOSubWriter_.AllocateSpaceForLoadCommands(aStream: TStream);
var buf : pbyte;
begin
  fHeader.sizeofcmds:=
    //segment+res section+bss section
    sizeof(_TSegmentCommand_)+sizeof(_TSection_)*2+
    //symbol table and dynamic symbol table commands
    sizeof(TSymtabCommand)+sizeof(TDySymtabCommand)+
    //common header of the three commands
    sizeof(TLoadCommand)*3;
  buf:=GetMem(fHeader.sizeofcmds);
  FillByte(buf^,fHeader.sizeofcmds,0);
  try
    aStream.WriteBuffer(buf^,fHeader.sizeofcmds);
  finally
    FreeMem(buf);
  end;
end;

procedure _TMachOSubWriter_.FixLoadCommands(aStream: TStream; aResources : TResources);
var ldcommand : TLoadCommand;
    segcommand : _TSegmentCommand_;
    symcommand : TSymtabCommand;
    dysymcommand : TDySymtabCommand;
    ressection,bsssection : _TSection_;
begin
  ldcommand.cmd:=fSegType;
  ldcommand.cmdsize:=sizeof(TLoadCommand)+sizeof(segcommand)+sizeof(ressection)*2;

  FillByte(segcommand.name[0],16,0);
  segcommand.vmaddr:=0;
  segcommand.vmsize:=fDataCurOfs+sizeof(_ptrtype_)*aResources.Count;
  segcommand.fileoff:=fSectionStart;
  segcommand.filesize:=fDataCurOfs;
  segcommand.maxprot:=VM_PROT_READ or VM_PROT_WRITE;
  segcommand.initprot:=VM_PROT_READ or VM_PROT_WRITE;
  segcommand.nsects:=2;
  segcommand.flags:=0;

  ressection.sectname:=RsrcSectName;
  ressection.segname:=DataSegName;
  ressection.addr:=0;
  ressection.size:=segcommand.filesize;
  ressection.offset:=segcommand.fileoff;
  ressection.align:=fSectAlignment;
  ressection.reloff:=fRelocations.StartOfs;
  ressection.nreloc:=fRelocations.Count;
  ressection.flags:=S_ATTR_LOC_RELOC;
  ressection.reserved1:=0;
  ressection.reserved2:=0;

  bsssection.sectname:=HandlesSectName;
  bsssection.segname:=DataSegName;
  bsssection.addr:=fDataCurOfs;
  bsssection.size:=sizeof(_ptrtype_)*aResources.Count;
  bsssection.offset:=0;
  bsssection.align:=fSectAlignment;
  bsssection.reloff:=0;
  bsssection.nreloc:=0;
  bsssection.flags:=S_ZEROFILL;
  bsssection.reserved1:=0;
  bsssection.reserved2:=0;

  if fOppositeEndianess then
  begin
    ldcommand.cmd:=SwapEndian(ldcommand.cmd);
    ldcommand.cmdsize:=SwapEndian(ldcommand.cmdsize);

    segcommand.vmaddr:=SwapEndian(segcommand.vmaddr);
    segcommand.vmsize:=SwapEndian(segcommand.vmsize);
    segcommand.fileoff:=SwapEndian(segcommand.fileoff);
    segcommand.filesize:=SwapEndian(segcommand.filesize);
    segcommand.maxprot:=SwapEndian(segcommand.maxprot);
    segcommand.initprot:=SwapEndian(segcommand.initprot);
    segcommand.nsects:=SwapEndian(segcommand.nsects);
    segcommand.flags:=SwapEndian(segcommand.flags);

    SwapSection(ressection);
    SwapSection(bsssection);
  end;

  aStream.WriteBuffer(ldcommand,sizeof(ldcommand));
  aStream.WriteBuffer(segcommand,sizeof(segcommand));
  aStream.WriteBuffer(ressection,sizeof(ressection));
  aStream.WriteBuffer(bsssection,sizeof(bsssection));

  ldcommand.cmd:=LC_SYMTAB;
  ldcommand.cmdsize:=sizeof(TLoadCommand)+sizeof(symcommand);

  symcommand.symoff:=fSymbolTable.StartOfs;
  symcommand.nsyms:=fSymbolTable.Count;
  symcommand.stroff:=fMachOStringTable.StartOfs;
  symcommand.strsize:=NextAligned(fDataAlignment,fMachOStringTable.Size);

  if fOppositeEndianess then
  begin
    ldcommand.cmd:=SwapEndian(ldcommand.cmd);
    ldcommand.cmdsize:=SwapEndian(ldcommand.cmdsize);

    symcommand.symoff:=SwapEndian(symcommand.symoff);
    symcommand.nsyms:=SwapEndian(symcommand.nsyms);
    symcommand.stroff:=SwapEndian(symcommand.stroff);
    symcommand.strsize:=SwapEndian(symcommand.strsize);
  end;

  aStream.WriteBuffer(ldcommand,sizeof(ldcommand));
  aStream.WriteBuffer(symcommand,sizeof(symcommand));

  ldcommand.cmd:=LC_DYSYMTAB;
  ldcommand.cmdsize:=sizeof(TLoadCommand)+sizeof(dysymcommand);

  dysymcommand.ilocalsym:=0;
  dysymcommand.nlocalsym:=fSymbolTable.LocalCount;
  dysymcommand.iextdefsym:=dysymcommand.ilocalsym+dysymcommand.nlocalsym;
  dysymcommand.nextdefsym:=fSymbolTable.GlobalCount;
  dysymcommand.iundefsym:=dysymcommand.iextdefsym+dysymcommand.nextdefsym;
  dysymcommand.nundefsym:=0;
  dysymcommand.tocoff:=0;
  dysymcommand.ntoc:=0;
  dysymcommand.modtaboff:=0;
  dysymcommand.nmodtab:=0;
  dysymcommand.extrefsymoff:=0;
  dysymcommand.nextrefsyms:=0;
  dysymcommand.indirectsymoff:=0;
  dysymcommand.nindirectsyms:=0;
  dysymcommand.extreloff:=0;
  dysymcommand.nextrel:=0;
  dysymcommand.locreloff:=0;
  dysymcommand.nlocrel:=0;

  if fOppositeEndianess then
  begin
    ldcommand.cmd:=SwapEndian(ldcommand.cmd);
    ldcommand.cmdsize:=SwapEndian(ldcommand.cmdsize);

    dysymcommand.ilocalsym:=SwapEndian(dysymcommand.ilocalsym);
    dysymcommand.nlocalsym:=SwapEndian(dysymcommand.nlocalsym);
    dysymcommand.iextdefsym:=SwapEndian(dysymcommand.iextdefsym);
    dysymcommand.nextdefsym:=SwapEndian(dysymcommand.nextdefsym);
    dysymcommand.iundefsym:=SwapEndian(dysymcommand.iundefsym);
    dysymcommand.nundefsym:=SwapEndian(dysymcommand.nundefsym);
    dysymcommand.tocoff:=SwapEndian(dysymcommand.tocoff);
    dysymcommand.ntoc:=SwapEndian(dysymcommand.ntoc);
    dysymcommand.modtaboff:=SwapEndian(dysymcommand.modtaboff);
    dysymcommand.nmodtab:=SwapEndian(dysymcommand.nmodtab);
    dysymcommand.extrefsymoff:=SwapEndian(dysymcommand.extrefsymoff);
    dysymcommand.nextrefsyms:=SwapEndian(dysymcommand.nextrefsyms);
    dysymcommand.indirectsymoff:=SwapEndian(dysymcommand.indirectsymoff);
    dysymcommand.nindirectsyms:=SwapEndian(dysymcommand.nindirectsyms);
    dysymcommand.extreloff:=SwapEndian(dysymcommand.extreloff);
    dysymcommand.nextrel:=SwapEndian(dysymcommand.nextrel);
    dysymcommand.locreloff:=SwapEndian(dysymcommand.locreloff);
    dysymcommand.nlocrel:=SwapEndian(dysymcommand.nlocrel);
  end;

  aStream.WriteBuffer(ldcommand,sizeof(ldcommand));
  aStream.WriteBuffer(dysymcommand,sizeof(dysymcommand));
end;

procedure _TMachOSubWriter_.FixResHeader(aStream : TStream);
var hdr : _TResHdr_;
begin
  hdr.handles:=fDataCurOfs;
  if fOppositeEndianess then
    hdr.handles:=SwapEndian(hdr.handles);
  aStream.Seek(sizeof(hdr.rootptr)+sizeof(hdr.count)+sizeof(hdr.usedhandles),
    soFromCurrent);
  aStream.WriteBuffer(hdr.handles,sizeof(hdr.handles));
end;

constructor _TMachOSubWriter_.Create(aParent : TMachOResourceWriter;
  const aMachineType : TMachOMachineType; const aSubMachineType: TMachoSubMachineType; const aOppositeEndianess : boolean);
begin
  inherited Create(aParent,aMachineType,aSubMachineType,aOppositeEndianess);
  fSymbolTable:=_TMachOSymbolTable_.Create(fMachOStringTable);
  fSymbolTable.OppositeEndianess:=fOppositeEndianess;
  {$IF _TMachOSubWriter_=TMachO32SubWriter}
  fDataAlignment:=4;
  fSectAlignment:=2; //2^2
  fSegType:=LC_SEGMENT;
  {$ELSE}
  fDataAlignment:=8;
  fSectAlignment:=3; //2^3
  fSegType:=LC_SEGMENT_64;
  {$ENDIF}
end;