-
Notifications
You must be signed in to change notification settings - Fork 7
/
Cofflib.c
424 lines (324 loc) · 15.9 KB
/
Cofflib.c
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/****************************************************************************/
/* COPYRIGHT NOTICE */
/****************************************************************************/
/* A PL/I Compiler for the Win32 platform. */
/* Copyright (C) 1997 - 2006 Hugh Gleaves. */
/* */
/* This program is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU General Public License */
/* as published by the Free Software Foundation; either version 2 */
/* */
/* 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. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to the Free Software */
/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 */
/* USA. */
/****************************************************************************/
/*--------------------------------------------------------------------------*/
/* PL/1 Compiler Release 1.0 */
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* Modification History */
/*--------------------------------------------------------------------------*/
/* Who When What */
/* ------------------------------------------------------------------------ */
/* HWG 20-06-91 Initial version of module. */
/* HWG 15-09-91 Dont create EXTDEF's for unreferenced entries. */
/* HWG 23-09-91 EXTDEF records did not have the TYPE byte present. */
/* HWG 29-09-91 COMENT records emmited with copyrite stuff etc. */
/* HWG 30-09-91 MODEND record function implemented (called by code.c */
/* HWG 30-09-91 LEDATA records were not referring to their SEGDEF */
/* records correctly. */
/* HWG 12-10-91 The stack segment is created in this module now. */
/* */
/* HWG 23-10-91 All SEGDEF records are now created prior to any of */
/* the LEDATA records. */
/* HWG 24-10-91 Fixups emitted for internal entry values. */
/* */
/* HWG 25-10-91 Fixups are emitted for external entries, and public */
/* and external object module refs are created for */
/* the outer block and external entries respectively. */
/* HWG 30-11-91 Dont generate fixups for unreferenced externals. */
/* */
/* HWG 20-06-93 Report all unreferenced names except those declared */
/* via include files. */
/* */
/* HWG 20-06-96 Added diag 150 about the allocation of labels. */
/* */
/* HWG 20-09-02 Major change to COFF 32 Bit support on NT. */
/* DOS .OBJ support removed completely. */
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* Functional Description */
/*--------------------------------------------------------------------------*/
/* This module is the storage allocator. It walks-thru the symbol table */
/* and creates segment definition records for each procedure, for the */
/* following: static storage, external names, constant storage. */
/* The records are written to the object file. */
/* LNAMES records are also produced for CODE and DATA. */
/* This phase also creates PUBDEF records for the outer block of the source */
/* file being compiled, and EXTDEF records for any external entries. */
/* The MODEND record is not written in here, but rather at the end of the */
/* code generation phase. */
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* R E P L A C E S T A T E M E N T S */
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* I N C L U D E F I L E S */
/*--------------------------------------------------------------------------*/
# include "c_types.h"
//# include "acbp.h"
# include "objdefs.h"
# include "nodes.h"
# include "tokens.h"
# include "stdio.h"
# include "stdlib.h"
# include "setjmp.h"
# include "fixup.h"
# include "string.h"
# include "coff.h"
# include "time.h"
/*--------------------------------------------------------------------------*/
/* E X T E R N A L V A R I A B L E S */
/*--------------------------------------------------------------------------*/
#include "globals.h"
/*--------------------------------------------------------------------------*/
/* I M P O R T E D F U N C T I O N S */
/*--------------------------------------------------------------------------*/
void report (int,char *,int);
/*--------------------------------------------------------------------------*/
/* I N T E R N A L V A R I A B L E S */
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* I N T E R N A L F U N C T I O N S */
/*--------------------------------------------------------------------------*/
ObjFile_ptr CoffCreateObject (void)
{
ObjFile_ptr ob_ptr;
ob_ptr = malloc(sizeof(ObjFile));
ob_ptr->hdr.NumberOfSections = 0;
ob_ptr->hdr.NumberOfSymbols = 0;
ob_ptr->hdr.PointerToSymbolTable = 0;
ob_ptr->hdr.Machine = IMAGE_FILE_MACHINE_I386;
ob_ptr->hdr.SizeOfOptionalHeader = 0;
ob_ptr->hdr.TimeDateStamp = time(NULL);
ob_ptr->hdr.Characteristics = IMAGE_FILE_32BIT_MACHINE +
IMAGE_FILE_BYTES_REVERSED_LO +
IMAGE_FILE_LINE_NUMS_STRIPPED;
ob_ptr->first_sym_ptr = NULL;
ob_ptr->string_ptr = NULL;
ob_ptr->string_space_left = 0;
return (ob_ptr);
}
Section_ptr CoffInsertSection (ObjFile_ptr ob_ptr)
{
Section_ptr sect_ptr;
WORD n;
ob_ptr->hdr.NumberOfSections++;
n = ob_ptr->hdr.NumberOfSections;
sect_ptr = malloc(sizeof(Section));
ob_ptr->sec_ptr[n] = sect_ptr;
sect_ptr->sect_num = n;
/*------------------------------------------------------------*/
/* Set the pointer to this section headers data block to NULL */
/* This will be assigned later in the compilation process. */
/*------------------------------------------------------------*/
sect_ptr->data_ptr = NULL;
sect_ptr->allocated_space = 0;
/*-----------------------------------------------------------------*/
/* These two pointers will point to a singly linked lits of nodes. */
/* The nodes contain a COFF relocation/symbol and a pointer to the */
/* next in the list. */
/*-----------------------------------------------------------------*/
sect_ptr->reloc_ptr = NULL;
sect_ptr->num_relocs = 0;
/*--------------------------------------------------*/
/* Init all remaining fields in the Section Header */
/*--------------------------------------------------*/
sect_ptr->sect.Misc.VirtualSize = 0;
sect_ptr->sect.VirtualAddress = 0;
sect_ptr->sect.SizeOfRawData = 0;
sect_ptr->sect.PointerToRawData = 0;
sect_ptr->sect.PointerToRelocations = 0;
sect_ptr->sect.PointerToLinenumbers = 0;
sect_ptr->sect.NumberOfRelocations = 0;
sect_ptr->sect.NumberOfLinenumbers = 0;
sect_ptr->sect.Characteristics = 0;
/*-----------------------------------------------------------------*/
/* Finally return a pointer to this new Section Header so that the */
/* caller can carry out further processing upon it. */
/*-----------------------------------------------------------------*/
return (sect_ptr);
}
long CoffInsertString (ObjFile_ptr ob_ptr, char * instr)
{
long offset;
char * next_ptr;
/*-------------------------------------------------------*/
/* If no strings have yet been stored in the string pool */
/* we will allocate 4K as our intial space. */
/*-------------------------------------------------------*/
if (ob_ptr->string_ptr == NULL)
{
ob_ptr->string_ptr = malloc(4096);
STRTABSIZE(ob_ptr) = 4;
ob_ptr->string_space_left = 4096 - 4;
}
/*----------------------------------------------------------*/
/* If there is insufficient space to store this string then */
/* lets increase the string pool by 4K. */
/*----------------------------------------------------------*/
if (ob_ptr->string_space_left <= (long)(strlen(instr)))
{
ob_ptr->string_ptr = realloc(ob_ptr->string_ptr,4096);
ob_ptr->string_space_left += 4096;
}
next_ptr = ob_ptr->string_ptr + STRTABSIZE(ob_ptr);
strcpy(next_ptr,instr);
offset = STRTABSIZE(ob_ptr);
ob_ptr->string_space_left -= (strlen(instr) + 1);
STRTABSIZE(ob_ptr) += (strlen(instr) + 1);
return(offset);
}
/*-------------------------------------------------------------------*/
/* Allocate and append a new CoffSym node to the list in the obj hdr */
/*-------------------------------------------------------------------*/
CoffSym_ptr CoffCreateSymbol (ObjFile_ptr ob_ptr)
{
CoffSym_ptr last_sym_ptr;
if (ob_ptr->first_sym_ptr == NULL)
{
ob_ptr->first_sym_ptr = malloc(sizeof(CoffSym));
ob_ptr->first_sym_ptr->sym_idx = 0;
ob_ptr->first_sym_ptr->next_ptr = NULL;
memset(ob_ptr->first_sym_ptr->sym.Name.ShortName,0,8);
ob_ptr->first_sym_ptr->sym.NumberOfAuxSymbols = 0;
ob_ptr->first_sym_ptr->sym.SectionNumber = 0;
ob_ptr->first_sym_ptr->sym.StorageClass = 0;
ob_ptr->first_sym_ptr->sym.Type = 0;
ob_ptr->first_sym_ptr->sym.Value = 0;
return(ob_ptr->first_sym_ptr);
}
last_sym_ptr = ob_ptr->first_sym_ptr;
while (last_sym_ptr->next_ptr != NULL)
{
last_sym_ptr = last_sym_ptr->next_ptr;
}
last_sym_ptr->next_ptr = malloc(sizeof(CoffSym));
last_sym_ptr->next_ptr->sym_idx = last_sym_ptr->sym_idx + 1;
last_sym_ptr->next_ptr->next_ptr = NULL;
memset(last_sym_ptr->next_ptr->sym.Name.ShortName,0,8);
last_sym_ptr->next_ptr->sym.NumberOfAuxSymbols = 0;
last_sym_ptr->next_ptr->sym.SectionNumber = 0;
last_sym_ptr->next_ptr->sym.StorageClass = 0;
last_sym_ptr->next_ptr->sym.Type = 0;
last_sym_ptr->next_ptr->sym.Value = 0;
return(last_sym_ptr->next_ptr);
}
/*-----------------------------------------------------------------*/
/* This function returns a pointer to the specified section header */
/*-----------------------------------------------------------------*/
Section_ptr CoffGetSectionPtr (ObjFile_ptr ob_ptr,short sect_num)
{
return(ob_ptr->sec_ptr[sect_num]);
}
/*--------------------------------------------------------------------------*/
/* Write all object file data to the file. This is the header, sections etc */
/*--------------------------------------------------------------------------*/
void CoffWriteObjFile (ObjFile_ptr ob_root)
{
short S,N;
Section_ptr sect_ptr,prev_ptr;
CoffSym_ptr sym_ptr;
CoffRel_ptr rel_ptr;
CoffReloc_ptr reloc_ptr;
long size_of_relocs;
size_t length;
//long file_posn;
/* Write the header */
fwrite (&(ob_root->hdr),sizeof(CoffHdr),1,OB);
N = ob_root->hdr.NumberOfSections;
sect_ptr = ob_root->sec_ptr[1];
sect_ptr->sect.PointerToRawData = sizeof(CoffHdr) + (sizeof(SectHdr) * N);
if (sect_ptr->sect.NumberOfRelocations > 0)
sect_ptr->sect.PointerToRelocations = sect_ptr->sect.PointerToRawData + sect_ptr->sect.SizeOfRawData;
fwrite (&(sect_ptr->sect),sizeof(SectHdr),1,OB);
for (S=2; S <= N; S++)
{
prev_ptr = sect_ptr;
size_of_relocs = prev_ptr->sect.NumberOfRelocations * sizeof(CoffReloc);
sect_ptr = ob_root->sec_ptr[S];
sect_ptr->sect.PointerToRawData = prev_ptr->sect.PointerToRawData + prev_ptr->sect.SizeOfRawData + size_of_relocs;
if (sect_ptr->sect.NumberOfRelocations > 0)
sect_ptr->sect.PointerToRelocations = sect_ptr->sect.PointerToRawData + sect_ptr->sect.SizeOfRawData;
else
sect_ptr->sect.PointerToRelocations = 0;
fwrite (&(sect_ptr->sect),sizeof(SectHdr),1,OB);
}
for (S=1; S <= N; S++)
{
sect_ptr = ob_root->sec_ptr[S];
if (sect_ptr->data_ptr != NULL)
{
fwrite ((sect_ptr->data_ptr),sect_ptr->sect.SizeOfRawData,1,OB);
rel_ptr = sect_ptr->reloc_ptr;
length = sizeof(CoffReloc);
while (rel_ptr != NULL)
{
reloc_ptr = &(rel_ptr->reloc);
fwrite(reloc_ptr,length,1,OB);
rel_ptr = rel_ptr->next_ptr;
}
}
}
/*------------------------------------------------------------------------------------*/
/* OK We now write out the symbol table entries. We first do an ftell to get the byte */
/* offset at which we are going to begin doing this, this needs to go in the header. */
/*------------------------------------------------------------------------------------*/
if (ob_root->first_sym_ptr != NULL)
{
ob_root->hdr.PointerToSymbolTable = ftell(OB);
fwrite (&(ob_root->first_sym_ptr->sym),18,/* sizeof(CoffSymbol) */ 1,OB);
sym_ptr = ob_root->first_sym_ptr->next_ptr;
while (sym_ptr != NULL)
{
fwrite (&(sym_ptr->sym),18,/* sizeof(CoffSymbol) */ 1,OB);
ob_root->hdr.NumberOfSymbols = sym_ptr->sym_idx + 1;
sym_ptr = sym_ptr->next_ptr;
}
}
/* OK Now write the string table */
if (ob_root->string_ptr != NULL)
fwrite (ob_root->string_ptr,STRTABSIZE(ob_root),1,OB);
/*-------------------------------------------------------------------------------------------*/
/* Finally reposition to the start of the file and re-write the header with its final values */
/*-------------------------------------------------------------------------------------------*/
//file_posn = ftell(OB);
fseek (OB,0,SEEK_SET);
fwrite (&(ob_root->hdr),sizeof(CoffHdr),1,OB);
//fseek (OB,file_posn,SEEK_SET);
}
void CoffInsertReloc (Section_ptr sect_ptr, CoffReloc_ptr reloc_ptr)
{
CoffRel_ptr temp_ptr;
if (sect_ptr->reloc_ptr == NULL)
{
sect_ptr->reloc_ptr = malloc(sizeof(CoffRel));
sect_ptr->sect.NumberOfRelocations = 1;
sect_ptr->num_relocs = 1;
sect_ptr->reloc_ptr->reloc = *(reloc_ptr);
sect_ptr->reloc_ptr->next_ptr = NULL;
return;
}
temp_ptr = malloc(sizeof(CoffRel));
temp_ptr->next_ptr = sect_ptr->reloc_ptr;
sect_ptr->sect.NumberOfRelocations += 1;
sect_ptr->num_relocs += 1;
sect_ptr->reloc_ptr = temp_ptr;
sect_ptr->reloc_ptr->reloc = *(reloc_ptr);
}