1 | /* $Id: VBoxBs3ObjConverter.cpp 60527 2016-04-18 09:11:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Validation Kit - Boot Sector 3 object file convert.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <string.h>
|
---|
33 | #include <stdlib.h>
|
---|
34 | #include <errno.h>
|
---|
35 | #include <iprt/types.h>
|
---|
36 | #include <iprt/ctype.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/sort.h>
|
---|
39 | #include <iprt/x86.h>
|
---|
40 |
|
---|
41 | #include <iprt/formats/elf64.h>
|
---|
42 | #include <iprt/formats/elf-amd64.h>
|
---|
43 | #include <iprt/formats/pecoff.h>
|
---|
44 | #include <iprt/formats/omf.h>
|
---|
45 | #include <iprt/formats/codeview.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | /*********************************************************************************************************************************
|
---|
49 | * Defined Constants And Macros *
|
---|
50 | *********************************************************************************************************************************/
|
---|
51 | #if ARCH_BITS == 64 && !defined(RT_OS_WINDOWS) && !defined(RT_OS_DARWIN)
|
---|
52 | # define ELF_FMT_X64 "lx"
|
---|
53 | # define ELF_FMT_D64 "ld"
|
---|
54 | #else
|
---|
55 | # define ELF_FMT_X64 "llx"
|
---|
56 | # define ELF_FMT_D64 "lld"
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | /** Compares an OMF string with a constant string. */
|
---|
60 | #define IS_OMF_STR_EQUAL_EX(a_cch1, a_pch1, a_szConst2) \
|
---|
61 | ( (a_cch1) == sizeof(a_szConst2) - 1 && memcmp(a_pch1, a_szConst2, sizeof(a_szConst2) - 1) == 0 )
|
---|
62 |
|
---|
63 | /** Compares an OMF string with a constant string. */
|
---|
64 | #define IS_OMF_STR_EQUAL(a_pchZeroPrefixed, a_szConst2) \
|
---|
65 | IS_OMF_STR_EQUAL_EX((uint8_t)((a_pchZeroPrefixed)[0]), &((a_pchZeroPrefixed)[1]), a_szConst2)
|
---|
66 |
|
---|
67 |
|
---|
68 | /*********************************************************************************************************************************
|
---|
69 | * Global Variables *
|
---|
70 | *********************************************************************************************************************************/
|
---|
71 | /** Verbosity level. */
|
---|
72 | static unsigned g_cVerbose = 0;
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Opens a file for binary reading or writing.
|
---|
77 | *
|
---|
78 | * @returns File stream handle.
|
---|
79 | * @param pszFile The name of the file.
|
---|
80 | * @param fWrite Whether to open for writing or reading.
|
---|
81 | */
|
---|
82 | static FILE *openfile(const char *pszFile, bool fWrite)
|
---|
83 | {
|
---|
84 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
85 | FILE *pFile = fopen(pszFile, fWrite ? "wb" : "rb");
|
---|
86 | #else
|
---|
87 | FILE *pFile = fopen(pszFile, fWrite ? "w" : "r");
|
---|
88 | #endif
|
---|
89 | if (!pFile)
|
---|
90 | fprintf(stderr, "error: Failed to open '%s' for %s: %s (%d)\n",
|
---|
91 | pszFile, fWrite ? "writing" : "reading", strerror(errno), errno);
|
---|
92 | return pFile;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Read the given file into memory.
|
---|
98 | *
|
---|
99 | * @returns true on success, false on failure.
|
---|
100 | * @param pszFile The file to read.
|
---|
101 | * @param ppvFile Where to return the memory.
|
---|
102 | * @param pcbFile Where to return the size.
|
---|
103 | */
|
---|
104 | static bool readfile(const char *pszFile, void **ppvFile, size_t *pcbFile)
|
---|
105 | {
|
---|
106 | FILE *pFile = openfile(pszFile, false);
|
---|
107 | if (pFile)
|
---|
108 | {
|
---|
109 | /*
|
---|
110 | * Figure the size.
|
---|
111 | */
|
---|
112 | if (fseek(pFile, 0, SEEK_END) == 0)
|
---|
113 | {
|
---|
114 | long cbFile = ftell(pFile);
|
---|
115 | if (cbFile > 0)
|
---|
116 | {
|
---|
117 | if (fseek(pFile, SEEK_SET, 0) == 0)
|
---|
118 | {
|
---|
119 | /*
|
---|
120 | * Allocate and read content.
|
---|
121 | */
|
---|
122 | void *pvFile = malloc((size_t)cbFile);
|
---|
123 | if (pvFile)
|
---|
124 | {
|
---|
125 | if (fread(pvFile, cbFile, 1, pFile) == 1)
|
---|
126 | {
|
---|
127 | *ppvFile = pvFile;
|
---|
128 | *pcbFile = (size_t)cbFile;
|
---|
129 | fclose(pFile);
|
---|
130 | return true;
|
---|
131 | }
|
---|
132 | free(pvFile);
|
---|
133 | fprintf(stderr, "error: fread failed in '%s': %s (%d)\n", pszFile, strerror(errno), errno);
|
---|
134 | }
|
---|
135 | else
|
---|
136 | fprintf(stderr, "error: failed to allocate %ld bytes of memory for '%s'\n", cbFile, pszFile);
|
---|
137 | }
|
---|
138 | else
|
---|
139 | fprintf(stderr, "error: fseek #2 failed in '%s': %s (%d)\n", pszFile, strerror(errno), errno);
|
---|
140 | }
|
---|
141 | else
|
---|
142 | fprintf(stderr, "error: ftell failed in '%s': %s (%d)\n", pszFile, strerror(errno), errno);
|
---|
143 | }
|
---|
144 | else
|
---|
145 | fprintf(stderr, "error: fseek #1 failed in '%s': %s (%d)\n", pszFile, strerror(errno), errno);
|
---|
146 | fclose(pFile);
|
---|
147 | }
|
---|
148 | return false;
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Write the given file into memory.
|
---|
154 | *
|
---|
155 | * @returns true on success, false on failure.
|
---|
156 | * @param pszFile The file to write.
|
---|
157 | * @param pvFile Where to return the memory.
|
---|
158 | * @param cbFile Where to return the size.
|
---|
159 | */
|
---|
160 | static bool writefile(const char *pszFile, void const *pvFile, size_t cbFile)
|
---|
161 | {
|
---|
162 | remove(pszFile);
|
---|
163 |
|
---|
164 | int rc = -1;
|
---|
165 | FILE *pFile = openfile(pszFile, true);
|
---|
166 | if (pFile)
|
---|
167 | {
|
---|
168 | if (fwrite(pvFile, cbFile, 1, pFile) == 1)
|
---|
169 | {
|
---|
170 | fclose(pFile);
|
---|
171 | return true;
|
---|
172 | }
|
---|
173 | fprintf(stderr, "error: fwrite failed in '%s': %s (%d)\n", pszFile, strerror(errno), errno);
|
---|
174 | fclose(pFile);
|
---|
175 | }
|
---|
176 | return false;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Reports an error and returns false.
|
---|
182 | *
|
---|
183 | * @returns false
|
---|
184 | * @param pszFile The filename.
|
---|
185 | * @param pszFormat The message format string.
|
---|
186 | * @param ... Format arguments.
|
---|
187 | */
|
---|
188 | static bool error(const char *pszFile, const char *pszFormat, ...)
|
---|
189 | {
|
---|
190 | fflush(stdout);
|
---|
191 | fprintf(stderr, "error: %s: ", pszFile);
|
---|
192 | va_list va;
|
---|
193 | va_start(va, pszFormat);
|
---|
194 | vfprintf(stderr, pszFormat, va);
|
---|
195 | va_end(va);
|
---|
196 | return false;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 |
|
---|
201 | /*********************************************************************************************************************************
|
---|
202 | * Common OMF Writer *
|
---|
203 | *********************************************************************************************************************************/
|
---|
204 |
|
---|
205 | /** Entry for each segment/section in the source format for mapping it to a
|
---|
206 | * segment defintion. */
|
---|
207 | typedef struct OMFTOSEGDEF
|
---|
208 | {
|
---|
209 | /** The segment defintion index of the section, UINT16_MAX if not translated. */
|
---|
210 | uint16_t iSegDef;
|
---|
211 | /** The group index for this segment, UINT16_MAX if not applicable. */
|
---|
212 | uint16_t iGrpDef;
|
---|
213 | /** The class name table entry, UINT16_MAX if not applicable. */
|
---|
214 | uint16_t iClassNm;
|
---|
215 | /** The group name for this segment, UINT16_MAX if not applicable. */
|
---|
216 | uint16_t iGrpNm;
|
---|
217 | /** The group name for this segment, UINT16_MAX if not applicable. */
|
---|
218 | uint16_t iSegNm;
|
---|
219 | /** The number of public definitions for this segment. */
|
---|
220 | uint32_t cPubDefs;
|
---|
221 | /** The segment name (OMF). */
|
---|
222 | char *pszName;
|
---|
223 | } OMFTOSEGDEF;
|
---|
224 | /** Pointer to a segment/section to segdef mapping. */
|
---|
225 | typedef OMFTOSEGDEF *POMFTOSEGDEF;
|
---|
226 |
|
---|
227 | /** Symbol table translation type. */
|
---|
228 | typedef enum OMFSYMTYPE
|
---|
229 | {
|
---|
230 | /** Invalid symbol table entry (aux sym). */
|
---|
231 | OMFSYMTYPE_INVALID = 0,
|
---|
232 | /** Ignored. */
|
---|
233 | OMFSYMTYPE_IGNORED,
|
---|
234 | /** A public defintion. */
|
---|
235 | OMFSYMTYPE_PUBDEF,
|
---|
236 | /** An external definition. */
|
---|
237 | OMFSYMTYPE_EXTDEF,
|
---|
238 | /** A segment reference for fixups. */
|
---|
239 | OMFSYMTYPE_SEGDEF,
|
---|
240 | /** Internal symbol that may be used for fixups. */
|
---|
241 | OMFSYMTYPE_INTERNAL
|
---|
242 | } OMFSYMTYPE;
|
---|
243 |
|
---|
244 | /** Symbol table translation. */
|
---|
245 | typedef struct OMFSYMBOL
|
---|
246 | {
|
---|
247 | /** What this source symbol table entry should be translated into. */
|
---|
248 | OMFSYMTYPE enmType;
|
---|
249 | /** The OMF table index. UINT16_MAX if not applicable. */
|
---|
250 | uint16_t idx;
|
---|
251 | /** The OMF segment definition index. */
|
---|
252 | uint16_t idxSegDef;
|
---|
253 | /** The OMF group definition index. */
|
---|
254 | uint16_t idxGrpDef;
|
---|
255 | } OMFSYMBOL;
|
---|
256 | /** Pointer to an source symbol table translation entry. */
|
---|
257 | typedef OMFSYMBOL *POMFSYMBOL;
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * OMF converter & writer instance.
|
---|
261 | */
|
---|
262 | typedef struct OMFWRITER
|
---|
263 | {
|
---|
264 | /** The source file name (for bitching). */
|
---|
265 | const char *pszSrc;
|
---|
266 | /** The destination output file. */
|
---|
267 | FILE *pDst;
|
---|
268 |
|
---|
269 | /** Pointer to the table mapping from source segments/section to segdefs. */
|
---|
270 | POMFTOSEGDEF paSegments;
|
---|
271 | /** Number of source segments/sections. */
|
---|
272 | uint32_t cSegments;
|
---|
273 |
|
---|
274 | /** Number of entries in the source symbol table. */
|
---|
275 | uint32_t cSymbols;
|
---|
276 | /** Pointer to the table mapping from source symbols to OMF stuff. */
|
---|
277 | POMFSYMBOL paSymbols;
|
---|
278 |
|
---|
279 | /** LEDATA segment offset. */
|
---|
280 | uint32_t offSeg;
|
---|
281 | /** Start of the current LEDATA record. */
|
---|
282 | uint32_t offSegRec;
|
---|
283 | /** The LEDATA end segment offset. */
|
---|
284 | uint32_t offSegEnd;
|
---|
285 | /** The current LEDATA segment. */
|
---|
286 | uint16_t idx;
|
---|
287 |
|
---|
288 | /** The index of the next list of names entry. */
|
---|
289 | uint16_t idxNextName;
|
---|
290 |
|
---|
291 | /** The current record size. */
|
---|
292 | uint16_t cbRec;
|
---|
293 | /** The current record type */
|
---|
294 | uint8_t bType;
|
---|
295 | /** The record data buffer (too large, but whatever). */
|
---|
296 | uint8_t abData[_1K + 64];
|
---|
297 |
|
---|
298 | /** Current FIXUPP entry. */
|
---|
299 | uint8_t iFixupp;
|
---|
300 | /** FIXUPP records being prepared for LEDATA currently stashed in abData.
|
---|
301 | * We may have to adjust addend values in the LEDATA when converting to OMF
|
---|
302 | * fixups. */
|
---|
303 | struct
|
---|
304 | {
|
---|
305 | uint16_t cbRec;
|
---|
306 | uint8_t abData[_1K + 64];
|
---|
307 | uint8_t abAlign[2]; /**< Alignment padding. */
|
---|
308 | } aFixupps[3];
|
---|
309 |
|
---|
310 | /** The index of the FLAT group. */
|
---|
311 | uint16_t idxGrpFlat;
|
---|
312 | /** The EXTDEF index of the __ImageBase symbol. */
|
---|
313 | uint16_t idxExtImageBase;
|
---|
314 | } OMFWRITE;
|
---|
315 | /** Pointer to an OMF writer. */
|
---|
316 | typedef OMFWRITE *POMFWRITER;
|
---|
317 |
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Creates an OMF writer instance.
|
---|
321 | */
|
---|
322 | static POMFWRITER omfWriter_Create(const char *pszSrc, uint32_t cSegments, uint32_t cSymbols, FILE *pDst)
|
---|
323 | {
|
---|
324 | POMFWRITER pThis = (POMFWRITER)calloc(sizeof(OMFWRITER), 1);
|
---|
325 | if (pThis)
|
---|
326 | {
|
---|
327 | pThis->pszSrc = pszSrc;
|
---|
328 | pThis->idxNextName = 1; /* We start counting at 1. */
|
---|
329 | pThis->cSegments = cSegments;
|
---|
330 | pThis->paSegments = (POMFTOSEGDEF)calloc(sizeof(OMFTOSEGDEF), cSegments);
|
---|
331 | if (pThis->paSegments)
|
---|
332 | {
|
---|
333 | pThis->cSymbols = cSymbols;
|
---|
334 | pThis->paSymbols = (POMFSYMBOL)calloc(sizeof(OMFSYMBOL), cSymbols);
|
---|
335 | if (pThis->paSymbols)
|
---|
336 | {
|
---|
337 | pThis->pDst = pDst;
|
---|
338 | return pThis;
|
---|
339 | }
|
---|
340 | free(pThis->paSegments);
|
---|
341 | }
|
---|
342 | free(pThis);
|
---|
343 | }
|
---|
344 | error(pszSrc, "Out of memory!\n");
|
---|
345 | return NULL;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * Destroys the given OMF writer instance.
|
---|
350 | * @param pThis OMF writer instance.
|
---|
351 | */
|
---|
352 | static void omfWriter_Destroy(POMFWRITER pThis)
|
---|
353 | {
|
---|
354 | free(pThis->paSymbols);
|
---|
355 | for (uint32_t i = 0; i < pThis->cSegments; i++)
|
---|
356 | if (pThis->paSegments[i].pszName)
|
---|
357 | free(pThis->paSegments[i].pszName);
|
---|
358 | free(pThis->paSegments);
|
---|
359 | free(pThis);
|
---|
360 | }
|
---|
361 |
|
---|
362 | static bool omfWriter_RecBegin(POMFWRITER pThis, uint8_t bType)
|
---|
363 | {
|
---|
364 | pThis->bType = bType;
|
---|
365 | pThis->cbRec = 0;
|
---|
366 | return true;
|
---|
367 | }
|
---|
368 |
|
---|
369 | static bool omfWriter_RecAddU8(POMFWRITER pThis, uint8_t b)
|
---|
370 | {
|
---|
371 | if (pThis->cbRec < OMF_MAX_RECORD_PAYLOAD)
|
---|
372 | {
|
---|
373 | pThis->abData[pThis->cbRec++] = b;
|
---|
374 | return true;
|
---|
375 | }
|
---|
376 | return error(pThis->pszSrc, "Exceeded max OMF record length (bType=%#x)!\n", pThis->bType);
|
---|
377 | }
|
---|
378 |
|
---|
379 | static bool omfWriter_RecAddU16(POMFWRITER pThis, uint16_t u16)
|
---|
380 | {
|
---|
381 | if (pThis->cbRec + 2U <= OMF_MAX_RECORD_PAYLOAD)
|
---|
382 | {
|
---|
383 | pThis->abData[pThis->cbRec++] = (uint8_t)u16;
|
---|
384 | pThis->abData[pThis->cbRec++] = (uint8_t)(u16 >> 8);
|
---|
385 | return true;
|
---|
386 | }
|
---|
387 | return error(pThis->pszSrc, "Exceeded max OMF record length (bType=%#x)!\n", pThis->bType);
|
---|
388 | }
|
---|
389 |
|
---|
390 | static bool omfWriter_RecAddU32(POMFWRITER pThis, uint32_t u32)
|
---|
391 | {
|
---|
392 | if (pThis->cbRec + 4U <= OMF_MAX_RECORD_PAYLOAD)
|
---|
393 | {
|
---|
394 | pThis->abData[pThis->cbRec++] = (uint8_t)u32;
|
---|
395 | pThis->abData[pThis->cbRec++] = (uint8_t)(u32 >> 8);
|
---|
396 | pThis->abData[pThis->cbRec++] = (uint8_t)(u32 >> 16);
|
---|
397 | pThis->abData[pThis->cbRec++] = (uint8_t)(u32 >> 24);
|
---|
398 | return true;
|
---|
399 | }
|
---|
400 | return error(pThis->pszSrc, "Exceeded max OMF record length (bType=%#x)!\n", pThis->bType);
|
---|
401 | }
|
---|
402 |
|
---|
403 | static bool omfWriter_RecAddIdx(POMFWRITER pThis, uint16_t idx)
|
---|
404 | {
|
---|
405 | if (idx < 128)
|
---|
406 | return omfWriter_RecAddU8(pThis, (uint8_t)idx);
|
---|
407 | if (idx < _32K)
|
---|
408 | return omfWriter_RecAddU8(pThis, (uint8_t)(idx >> 7) | 0x80)
|
---|
409 | && omfWriter_RecAddU8(pThis, (uint8_t)idx);
|
---|
410 | return error(pThis->pszSrc, "Index out of range %#x\n", idx);
|
---|
411 | }
|
---|
412 |
|
---|
413 | static bool omfWriter_RecAddBytes(POMFWRITER pThis, const void *pvData, size_t cbData)
|
---|
414 | {
|
---|
415 | const uint16_t cbNasmHack = OMF_MAX_RECORD_PAYLOAD + 1;
|
---|
416 | if (cbData + pThis->cbRec <= cbNasmHack)
|
---|
417 | {
|
---|
418 | memcpy(&pThis->abData[pThis->cbRec], pvData, cbData);
|
---|
419 | pThis->cbRec += (uint16_t)cbData;
|
---|
420 | return true;
|
---|
421 | }
|
---|
422 | return error(pThis->pszSrc, "Exceeded max OMF record length (bType=%#x, cbData=%#x, cbRec=%#x, max=%#x)!\n",
|
---|
423 | pThis->bType, (unsigned)cbData, pThis->cbRec, OMF_MAX_RECORD_PAYLOAD);
|
---|
424 | }
|
---|
425 |
|
---|
426 | static bool omfWriter_RecAddStringN(POMFWRITER pThis, const char *pchString, size_t cchString)
|
---|
427 | {
|
---|
428 | if (cchString < 256)
|
---|
429 | {
|
---|
430 | return omfWriter_RecAddU8(pThis, (uint8_t)cchString)
|
---|
431 | && omfWriter_RecAddBytes(pThis, pchString, cchString);
|
---|
432 | }
|
---|
433 | return error(pThis->pszSrc, "String too long (%u bytes): '%*.*s'\n",
|
---|
434 | (unsigned)cchString, (int)cchString, (int)cchString, pchString);
|
---|
435 | }
|
---|
436 |
|
---|
437 | static bool omfWriter_RecAddString(POMFWRITER pThis, const char *pszString)
|
---|
438 | {
|
---|
439 | return omfWriter_RecAddStringN(pThis, pszString, strlen(pszString));
|
---|
440 | }
|
---|
441 |
|
---|
442 | static bool omfWriter_RecEnd(POMFWRITER pThis, bool fAddCrc)
|
---|
443 | {
|
---|
444 | if ( !fAddCrc
|
---|
445 | || omfWriter_RecAddU8(pThis, 0))
|
---|
446 | {
|
---|
447 | OMFRECHDR RecHdr = { pThis->bType, RT_H2LE_U16(pThis->cbRec) };
|
---|
448 | if ( fwrite(&RecHdr, sizeof(RecHdr), 1, pThis->pDst) == 1
|
---|
449 | && fwrite(pThis->abData, pThis->cbRec, 1, pThis->pDst) == 1)
|
---|
450 | {
|
---|
451 | pThis->bType = 0;
|
---|
452 | pThis->cbRec = 0;
|
---|
453 | return true;
|
---|
454 | }
|
---|
455 | return error(pThis->pszSrc, "Write error\n");
|
---|
456 | }
|
---|
457 | return false;
|
---|
458 | }
|
---|
459 |
|
---|
460 | static bool omfWriter_RecEndWithCrc(POMFWRITER pThis)
|
---|
461 | {
|
---|
462 | return omfWriter_RecEnd(pThis, true /*fAddCrc*/);
|
---|
463 | }
|
---|
464 |
|
---|
465 |
|
---|
466 | static bool omfWriter_BeginModule(POMFWRITER pThis, const char *pszFile)
|
---|
467 | {
|
---|
468 | return omfWriter_RecBegin(pThis, OMF_THEADR)
|
---|
469 | && omfWriter_RecAddString(pThis, pszFile)
|
---|
470 | && omfWriter_RecEndWithCrc(pThis);
|
---|
471 | }
|
---|
472 |
|
---|
473 | static bool omfWriter_LNamesAddN(POMFWRITER pThis, const char *pchName, size_t cchName, uint16_t *pidxName)
|
---|
474 | {
|
---|
475 | /* split? */
|
---|
476 | if (pThis->cbRec + 1 /*len*/ + cchName + 1 /*crc*/ > OMF_MAX_RECORD_PAYLOAD)
|
---|
477 | {
|
---|
478 | if (pThis->cbRec == 0)
|
---|
479 | return error(pThis->pszSrc, "Too long LNAME '%*.*s'\n", (int)cchName, (int)cchName, pchName);
|
---|
480 | if ( !omfWriter_RecEndWithCrc(pThis)
|
---|
481 | || !omfWriter_RecBegin(pThis, OMF_LNAMES))
|
---|
482 | return false;
|
---|
483 | }
|
---|
484 |
|
---|
485 | if (pidxName)
|
---|
486 | *pidxName = pThis->idxNextName;
|
---|
487 | pThis->idxNextName++;
|
---|
488 | return omfWriter_RecAddStringN(pThis, pchName, cchName);
|
---|
489 | }
|
---|
490 |
|
---|
491 | static bool omfWriter_LNamesAdd(POMFWRITER pThis, const char *pszName, uint16_t *pidxName)
|
---|
492 | {
|
---|
493 | return omfWriter_LNamesAddN(pThis, pszName, strlen(pszName), pidxName);
|
---|
494 | }
|
---|
495 |
|
---|
496 | static bool omfWriter_LNamesBegin(POMFWRITER pThis, bool fAddZeroEntry)
|
---|
497 | {
|
---|
498 | /* First entry is an empty string. */
|
---|
499 | return omfWriter_RecBegin(pThis, OMF_LNAMES)
|
---|
500 | && ( pThis->idxNextName > 1
|
---|
501 | || !fAddZeroEntry
|
---|
502 | || omfWriter_LNamesAddN(pThis, "", 0, NULL));
|
---|
503 | }
|
---|
504 |
|
---|
505 | static bool omfWriter_LNamesEnd(POMFWRITER pThis)
|
---|
506 | {
|
---|
507 | return omfWriter_RecEndWithCrc(pThis);
|
---|
508 | }
|
---|
509 |
|
---|
510 |
|
---|
511 | static bool omfWriter_SegDef(POMFWRITER pThis, uint8_t bSegAttr, uint32_t cbSeg, uint16_t idxSegName, uint16_t idxSegClass)
|
---|
512 | {
|
---|
513 | return omfWriter_RecBegin(pThis, OMF_SEGDEF32)
|
---|
514 | && omfWriter_RecAddU8(pThis, bSegAttr)
|
---|
515 | && omfWriter_RecAddU32(pThis, cbSeg)
|
---|
516 | && omfWriter_RecAddIdx(pThis, idxSegName)
|
---|
517 | && omfWriter_RecAddIdx(pThis, idxSegClass)
|
---|
518 | && omfWriter_RecAddIdx(pThis, 1) /* overlay name index = NULL entry */
|
---|
519 | && omfWriter_RecEndWithCrc(pThis);
|
---|
520 | }
|
---|
521 |
|
---|
522 | static bool omfWriter_GrpDefBegin(POMFWRITER pThis, uint16_t idxGrpName)
|
---|
523 | {
|
---|
524 | return omfWriter_RecBegin(pThis, OMF_GRPDEF)
|
---|
525 | && omfWriter_RecAddIdx(pThis, idxGrpName);
|
---|
526 | }
|
---|
527 |
|
---|
528 | static bool omfWriter_GrpDefAddSegDef(POMFWRITER pThis, uint16_t idxSegDef)
|
---|
529 | {
|
---|
530 | return omfWriter_RecAddU8(pThis, 0xff)
|
---|
531 | && omfWriter_RecAddIdx(pThis, idxSegDef);
|
---|
532 | }
|
---|
533 |
|
---|
534 | static bool omfWriter_GrpDefEnd(POMFWRITER pThis)
|
---|
535 | {
|
---|
536 | return omfWriter_RecEndWithCrc(pThis);
|
---|
537 | }
|
---|
538 |
|
---|
539 |
|
---|
540 | static bool omfWriter_PubDefBegin(POMFWRITER pThis, uint16_t idxGrpDef, uint16_t idxSegDef)
|
---|
541 | {
|
---|
542 | return omfWriter_RecBegin(pThis, OMF_PUBDEF32)
|
---|
543 | && omfWriter_RecAddIdx(pThis, idxGrpDef)
|
---|
544 | && omfWriter_RecAddIdx(pThis, idxSegDef)
|
---|
545 | && ( idxSegDef != 0
|
---|
546 | || omfWriter_RecAddU16(pThis, 0));
|
---|
547 |
|
---|
548 | }
|
---|
549 |
|
---|
550 | static bool omfWriter_PubDefAddN(POMFWRITER pThis, uint32_t uValue, const char *pchString, size_t cchString)
|
---|
551 | {
|
---|
552 | /* Split? */
|
---|
553 | if (pThis->cbRec + 1 + cchString + 4 + 1 + 1 > OMF_MAX_RECORD_PAYLOAD)
|
---|
554 | {
|
---|
555 | if (cchString >= 256)
|
---|
556 | return error(pThis->pszSrc, "PUBDEF string too long %u ('%s')\n",
|
---|
557 | (unsigned)cchString, (int)cchString, (int)cchString, pchString);
|
---|
558 | if (!omfWriter_RecEndWithCrc(pThis))
|
---|
559 | return false;
|
---|
560 |
|
---|
561 | /* Figure out the initial data length. */
|
---|
562 | pThis->cbRec = 1 + ((pThis->abData[0] & 0x80) != 0);
|
---|
563 | if (pThis->abData[pThis->cbRec] != 0)
|
---|
564 | pThis->cbRec += 1 + ((pThis->abData[pThis->cbRec] & 0x80) != 0);
|
---|
565 | else
|
---|
566 | pThis->cbRec += 3;
|
---|
567 | pThis->bType = OMF_PUBDEF32;
|
---|
568 | }
|
---|
569 |
|
---|
570 | return omfWriter_RecAddStringN(pThis, pchString, cchString)
|
---|
571 | && omfWriter_RecAddU32(pThis, uValue)
|
---|
572 | && omfWriter_RecAddIdx(pThis, 0); /* type */
|
---|
573 | }
|
---|
574 |
|
---|
575 | static bool omfWriter_PubDefAdd(POMFWRITER pThis, uint32_t uValue, const char *pszString)
|
---|
576 | {
|
---|
577 | return omfWriter_PubDefAddN(pThis, uValue, pszString, strlen(pszString));
|
---|
578 | }
|
---|
579 |
|
---|
580 | static bool omfWriter_PubDefEnd(POMFWRITER pThis)
|
---|
581 | {
|
---|
582 | return omfWriter_RecEndWithCrc(pThis);
|
---|
583 | }
|
---|
584 |
|
---|
585 | /**
|
---|
586 | * EXTDEF - Begin record.
|
---|
587 | */
|
---|
588 | static bool omfWriter_ExtDefBegin(POMFWRITER pThis)
|
---|
589 | {
|
---|
590 | return omfWriter_RecBegin(pThis, OMF_EXTDEF);
|
---|
591 |
|
---|
592 | }
|
---|
593 |
|
---|
594 | /**
|
---|
595 | * EXTDEF - Add an entry, split record if necessary.
|
---|
596 | */
|
---|
597 | static bool omfWriter_ExtDefAddN(POMFWRITER pThis, const char *pchString, size_t cchString, uint16_t idxType)
|
---|
598 | {
|
---|
599 | /* Split? */
|
---|
600 | if (pThis->cbRec + 1 + cchString + 1 + 1 > OMF_MAX_RECORD_PAYLOAD)
|
---|
601 | {
|
---|
602 | if (cchString >= 256)
|
---|
603 | return error(pThis->pszSrc, "EXTDEF string too long %u ('%s')\n",
|
---|
604 | (unsigned)cchString, (int)cchString, (int)cchString, pchString);
|
---|
605 | if ( !omfWriter_RecEndWithCrc(pThis)
|
---|
606 | || !omfWriter_RecBegin(pThis, OMF_EXTDEF))
|
---|
607 | return false;
|
---|
608 | }
|
---|
609 |
|
---|
610 | return omfWriter_RecAddStringN(pThis, pchString, cchString)
|
---|
611 | && omfWriter_RecAddIdx(pThis, idxType); /* type */
|
---|
612 | }
|
---|
613 |
|
---|
614 | /**
|
---|
615 | * EXTDEF - Add an entry, split record if necessary.
|
---|
616 | */
|
---|
617 | static bool omfWriter_ExtDefAdd(POMFWRITER pThis, const char *pszString)
|
---|
618 | {
|
---|
619 | return omfWriter_ExtDefAddN(pThis, pszString, strlen(pszString), 0);
|
---|
620 | }
|
---|
621 |
|
---|
622 | /**
|
---|
623 | * EXTDEF - End of record.
|
---|
624 | */
|
---|
625 | static bool omfWriter_ExtDefEnd(POMFWRITER pThis)
|
---|
626 | {
|
---|
627 | return omfWriter_RecEndWithCrc(pThis);
|
---|
628 | }
|
---|
629 |
|
---|
630 | /**
|
---|
631 | * COMENT/LINK_PASS_SEP - Add a link pass separator comment.
|
---|
632 | */
|
---|
633 | static bool omfWriter_LinkPassSeparator(POMFWRITER pThis)
|
---|
634 | {
|
---|
635 | return omfWriter_RecBegin(pThis, OMF_COMENT)
|
---|
636 | && omfWriter_RecAddU8(pThis, OMF_CTYP_NO_LIST)
|
---|
637 | && omfWriter_RecAddU8(pThis, OMF_CCLS_LINK_PASS_SEP)
|
---|
638 | && omfWriter_RecAddU8(pThis, 1)
|
---|
639 | && omfWriter_RecEndWithCrc(pThis);
|
---|
640 | }
|
---|
641 |
|
---|
642 |
|
---|
643 | /**
|
---|
644 | * LEDATA + FIXUPP - Begin records.
|
---|
645 | */
|
---|
646 | static bool omfWriter_LEDataBegin(POMFWRITER pThis, uint16_t idxSeg, uint32_t offSeg)
|
---|
647 | {
|
---|
648 | if ( omfWriter_RecBegin(pThis, OMF_LEDATA32)
|
---|
649 | && omfWriter_RecAddIdx(pThis, idxSeg)
|
---|
650 | && omfWriter_RecAddU32(pThis, offSeg))
|
---|
651 | {
|
---|
652 | pThis->idx = idxSeg;
|
---|
653 | pThis->offSeg = offSeg;
|
---|
654 | pThis->offSegRec = offSeg;
|
---|
655 | pThis->offSegEnd = offSeg + OMF_MAX_RECORD_PAYLOAD - 1 /*CRC*/ - pThis->cbRec;
|
---|
656 | pThis->offSegEnd &= ~(uint32_t)7; /* qword align. */
|
---|
657 |
|
---|
658 | /* Reset the associated FIXUPP records. */
|
---|
659 | pThis->iFixupp = 0;
|
---|
660 | for (unsigned i = 0; i < RT_ELEMENTS(pThis->aFixupps); i++)
|
---|
661 | pThis->aFixupps[i].cbRec = 0;
|
---|
662 | return true;
|
---|
663 | }
|
---|
664 | return false;
|
---|
665 | }
|
---|
666 |
|
---|
667 | /**
|
---|
668 | * LEDATA + FIXUPP - Begin records.
|
---|
669 | */
|
---|
670 | static bool omfWriter_LEDataBeginEx(POMFWRITER pThis, uint16_t idxSeg, uint32_t offSeg,
|
---|
671 | uint32_t cbData, uint32_t cbRawData, void const *pbRawData, uint8_t **ppbData)
|
---|
672 | {
|
---|
673 | if ( omfWriter_RecBegin(pThis, OMF_LEDATA32)
|
---|
674 | && omfWriter_RecAddIdx(pThis, idxSeg)
|
---|
675 | && omfWriter_RecAddU32(pThis, offSeg))
|
---|
676 | {
|
---|
677 | if ( cbData <= _1K
|
---|
678 | && pThis->cbRec + cbData + 1 <= OMF_MAX_RECORD_PAYLOAD)
|
---|
679 | {
|
---|
680 | uint8_t *pbDst = &pThis->abData[pThis->cbRec];
|
---|
681 | if (ppbData)
|
---|
682 | *ppbData = pbDst;
|
---|
683 |
|
---|
684 | if (cbRawData)
|
---|
685 | memcpy(pbDst, pbRawData, RT_MIN(cbData, cbRawData));
|
---|
686 | if (cbData > cbRawData)
|
---|
687 | memset(&pbDst[cbRawData], 0, cbData - cbRawData);
|
---|
688 |
|
---|
689 | pThis->cbRec += cbData;
|
---|
690 | pThis->idx = idxSeg;
|
---|
691 | pThis->offSegRec = offSeg;
|
---|
692 | pThis->offSeg = offSeg + cbData;
|
---|
693 | pThis->offSegEnd = offSeg + cbData;
|
---|
694 |
|
---|
695 | /* Reset the associated FIXUPP records. */
|
---|
696 | pThis->iFixupp = 0;
|
---|
697 | for (unsigned i = 0; i < RT_ELEMENTS(pThis->aFixupps); i++)
|
---|
698 | pThis->aFixupps[i].cbRec = 0;
|
---|
699 | return true;
|
---|
700 | }
|
---|
701 | error(pThis->pszSrc, "Too much data for LEDATA record! (%#x)\n", (unsigned)cbData);
|
---|
702 | }
|
---|
703 | return false;
|
---|
704 | }
|
---|
705 |
|
---|
706 | /**
|
---|
707 | * LEDATA + FIXUPP - Add FIXUPP subrecord bytes, split if necessary.
|
---|
708 | */
|
---|
709 | static bool omfWriter_LEDataAddFixuppBytes(POMFWRITER pThis, void *pvSubRec, size_t cbSubRec)
|
---|
710 | {
|
---|
711 | /* Split? */
|
---|
712 | unsigned iFixupp = pThis->iFixupp;
|
---|
713 | if (pThis->aFixupps[iFixupp].cbRec + cbSubRec >= OMF_MAX_RECORD_PAYLOAD)
|
---|
714 | {
|
---|
715 | if (g_cVerbose >= 2)
|
---|
716 | printf("debug: FIXUPP split\n");
|
---|
717 | iFixupp++;
|
---|
718 | if (iFixupp >= RT_ELEMENTS(pThis->aFixupps))
|
---|
719 | return error(pThis->pszSrc, "Out of FIXUPP records\n");
|
---|
720 | pThis->iFixupp = iFixupp;
|
---|
721 | pThis->aFixupps[iFixupp].cbRec = 0; /* paranoia */
|
---|
722 | }
|
---|
723 |
|
---|
724 | /* Append the sub-record data. */
|
---|
725 | memcpy(&pThis->aFixupps[iFixupp].abData[pThis->aFixupps[iFixupp].cbRec], pvSubRec, cbSubRec);
|
---|
726 | pThis->aFixupps[iFixupp].cbRec += (uint16_t)cbSubRec;
|
---|
727 | return true;
|
---|
728 | }
|
---|
729 |
|
---|
730 | /**
|
---|
731 | * LEDATA + FIXUPP - Add fixup, split if necessary.
|
---|
732 | */
|
---|
733 | static bool omfWriter_LEDataAddFixup(POMFWRITER pThis, uint16_t offDataRec, bool fSelfRel, uint8_t bLocation,
|
---|
734 | uint8_t bFrame, uint16_t idxFrame,
|
---|
735 | uint8_t bTarget, uint16_t idxTarget, bool fTargetDisp, uint32_t offTargetDisp)
|
---|
736 | {
|
---|
737 | if (g_cVerbose >= 2)
|
---|
738 | printf("debug: FIXUP[%#x]: off=%#x frame=%u:%#x target=%u:%#x disp=%d:%#x\n", pThis->aFixupps[pThis->iFixupp].cbRec,
|
---|
739 | offDataRec, bFrame, idxFrame, bTarget, idxTarget, fTargetDisp, offTargetDisp);
|
---|
740 |
|
---|
741 | if ( offDataRec >= _1K
|
---|
742 | || bFrame >= 6
|
---|
743 | || bTarget > 6
|
---|
744 | || idxFrame >= _32K
|
---|
745 | || idxTarget >= _32K
|
---|
746 | || fTargetDisp != (bTarget <= OMF_FIX_T_FRAME_NO) )
|
---|
747 | return error(pThis->pszSrc,
|
---|
748 | "Internal error: offDataRec=%#x bFrame=%u idxFrame=%#x bTarget=%u idxTarget=%#x fTargetDisp=%d offTargetDisp=%#x\n",
|
---|
749 | offDataRec, bFrame, idxFrame, bTarget, idxTarget, fTargetDisp, offTargetDisp);
|
---|
750 |
|
---|
751 |
|
---|
752 | /*
|
---|
753 | * Encode the FIXUP subrecord.
|
---|
754 | */
|
---|
755 | uint8_t abFixup[16];
|
---|
756 | uint8_t off = 0;
|
---|
757 | /* Location */
|
---|
758 | abFixup[off++] = (offDataRec >> 8) | (bLocation << 2) | ((uint8_t)!fSelfRel << 6) | 0x80;
|
---|
759 | abFixup[off++] = (uint8_t)offDataRec;
|
---|
760 | /* Fix Data */
|
---|
761 | abFixup[off++] = 0x00 /*F=0*/ | (bFrame << 4) | 0x00 /*T=0*/ | bTarget;
|
---|
762 | /* Frame Datum */
|
---|
763 | if (bFrame <= OMF_FIX_F_FRAME_NO)
|
---|
764 | {
|
---|
765 | if (idxFrame >= 128)
|
---|
766 | abFixup[off++] = (uint8_t)(idxFrame >> 8);
|
---|
767 | abFixup[off++] = (uint8_t)idxFrame;
|
---|
768 | }
|
---|
769 | /* Target Datum */
|
---|
770 | if (idxTarget >= 128)
|
---|
771 | abFixup[off++] = (uint8_t)(idxTarget >> 8);
|
---|
772 | abFixup[off++] = (uint8_t)idxTarget;
|
---|
773 | /* Target Displacement */
|
---|
774 | if (fTargetDisp)
|
---|
775 | {
|
---|
776 | abFixup[off++] = RT_BYTE1(offTargetDisp);
|
---|
777 | abFixup[off++] = RT_BYTE2(offTargetDisp);
|
---|
778 | abFixup[off++] = RT_BYTE3(offTargetDisp);
|
---|
779 | abFixup[off++] = RT_BYTE4(offTargetDisp);
|
---|
780 | }
|
---|
781 |
|
---|
782 | return omfWriter_LEDataAddFixuppBytes(pThis, abFixup, off);
|
---|
783 | }
|
---|
784 |
|
---|
785 | /**
|
---|
786 | * LEDATA + FIXUPP - Add simple fixup, split if necessary.
|
---|
787 | */
|
---|
788 | static bool omfWriter_LEDataAddFixupNoDisp(POMFWRITER pThis, uint16_t offDataRec, uint8_t bLocation,
|
---|
789 | uint8_t bFrame, uint16_t idxFrame, uint8_t bTarget, uint16_t idxTarget)
|
---|
790 | {
|
---|
791 | return omfWriter_LEDataAddFixup(pThis, offDataRec, false /*fSelfRel*/, bLocation, bFrame, idxFrame, bTarget, idxTarget,
|
---|
792 | false /*fTargetDisp*/, 0 /*offTargetDisp*/);
|
---|
793 | }
|
---|
794 |
|
---|
795 |
|
---|
796 | /**
|
---|
797 | * LEDATA + FIXUPP - End of records.
|
---|
798 | */
|
---|
799 | static bool omfWriter_LEDataEnd(POMFWRITER pThis)
|
---|
800 | {
|
---|
801 | if (omfWriter_RecEndWithCrc(pThis))
|
---|
802 | {
|
---|
803 | for (unsigned iFixupp = 0; iFixupp <= pThis->iFixupp; iFixupp++)
|
---|
804 | {
|
---|
805 | uint16_t const cbRec = pThis->aFixupps[iFixupp].cbRec;
|
---|
806 | if (!cbRec)
|
---|
807 | break;
|
---|
808 | if (g_cVerbose >= 3)
|
---|
809 | printf("debug: FIXUPP32 #%u cbRec=%#x\n", iFixupp, cbRec);
|
---|
810 | if ( !omfWriter_RecBegin(pThis, OMF_FIXUPP32)
|
---|
811 | || !omfWriter_RecAddBytes(pThis, pThis->aFixupps[iFixupp].abData, cbRec)
|
---|
812 | || !omfWriter_RecEndWithCrc(pThis))
|
---|
813 | return false;
|
---|
814 | }
|
---|
815 | pThis->iFixupp = 0;
|
---|
816 | return true;
|
---|
817 | }
|
---|
818 | return false;
|
---|
819 | }
|
---|
820 |
|
---|
821 | /**
|
---|
822 | * LEDATA + FIXUPP - Splits the LEDATA record.
|
---|
823 | */
|
---|
824 | static bool omfWriter_LEDataSplit(POMFWRITER pThis)
|
---|
825 | {
|
---|
826 | return omfWriter_LEDataEnd(pThis)
|
---|
827 | && omfWriter_LEDataBegin(pThis, pThis->idx, pThis->offSeg);
|
---|
828 | }
|
---|
829 |
|
---|
830 | /**
|
---|
831 | * LEDATA + FIXUPP - Returns available space in current LEDATA record.
|
---|
832 | */
|
---|
833 | static uint32_t omfWriter_LEDataAvailable(POMFWRITER pThis)
|
---|
834 | {
|
---|
835 | if (pThis->offSeg < pThis->offSegEnd)
|
---|
836 | return pThis->offSegEnd - pThis->offSeg;
|
---|
837 | return 0;
|
---|
838 | }
|
---|
839 |
|
---|
840 | /**
|
---|
841 | * LEDATA + FIXUPP - Splits LEDATA record if less than @a cb bytes available.
|
---|
842 | */
|
---|
843 | static bool omfWriter_LEDataEnsureSpace(POMFWRITER pThis, uint32_t cb)
|
---|
844 | {
|
---|
845 | if ( omfWriter_LEDataAvailable(pThis) >= cb
|
---|
846 | || omfWriter_LEDataSplit(pThis))
|
---|
847 | return true;
|
---|
848 | return false;
|
---|
849 | }
|
---|
850 |
|
---|
851 | /**
|
---|
852 | * LEDATA + FIXUPP - Adds data to the LEDATA record, splitting it if needed.
|
---|
853 | */
|
---|
854 | static bool omfWriter_LEDataAddBytes(POMFWRITER pThis, void const *pvData, size_t cbData)
|
---|
855 | {
|
---|
856 | while (cbData > 0)
|
---|
857 | {
|
---|
858 | uint32_t cbAvail = omfWriter_LEDataAvailable(pThis);
|
---|
859 | if (cbAvail >= cbData)
|
---|
860 | {
|
---|
861 | if (omfWriter_RecAddBytes(pThis, pvData, cbData))
|
---|
862 | {
|
---|
863 | pThis->offSeg += (uint32_t)cbData;
|
---|
864 | break;
|
---|
865 | }
|
---|
866 | return false;
|
---|
867 | }
|
---|
868 | if (!omfWriter_RecAddBytes(pThis, pvData, cbAvail))
|
---|
869 | return false;
|
---|
870 | pThis->offSeg += cbAvail;
|
---|
871 | pvData = (uint8_t const *)pvData + cbAvail;
|
---|
872 | cbData -= cbAvail;
|
---|
873 | if (!omfWriter_LEDataSplit(pThis))
|
---|
874 | return false;
|
---|
875 | }
|
---|
876 | return true;
|
---|
877 | }
|
---|
878 |
|
---|
879 | /**
|
---|
880 | * LEDATA + FIXUPP - Adds a U32 to the LEDATA record, splitting if needed.
|
---|
881 | */
|
---|
882 | static bool omfWriter_LEDataAddU32(POMFWRITER pThis, uint32_t u32)
|
---|
883 | {
|
---|
884 | if ( omfWriter_LEDataEnsureSpace(pThis, 4)
|
---|
885 | && omfWriter_RecAddU32(pThis, u32))
|
---|
886 | {
|
---|
887 | pThis->offSeg += 4;
|
---|
888 | return true;
|
---|
889 | }
|
---|
890 | return false;
|
---|
891 | }
|
---|
892 |
|
---|
893 | /**
|
---|
894 | * LEDATA + FIXUPP - Adds a U16 to the LEDATA record, splitting if needed.
|
---|
895 | */
|
---|
896 | static bool omfWriter_LEDataAddU16(POMFWRITER pThis, uint16_t u16)
|
---|
897 | {
|
---|
898 | if ( omfWriter_LEDataEnsureSpace(pThis, 2)
|
---|
899 | && omfWriter_RecAddU16(pThis, u16))
|
---|
900 | {
|
---|
901 | pThis->offSeg += 2;
|
---|
902 | return true;
|
---|
903 | }
|
---|
904 | return false;
|
---|
905 | }
|
---|
906 |
|
---|
907 | /**
|
---|
908 | * LEDATA + FIXUPP - Adds a byte to the LEDATA record, splitting if needed.
|
---|
909 | */
|
---|
910 | static bool omfWriter_LEDataAddU8(POMFWRITER pThis, uint8_t b)
|
---|
911 | {
|
---|
912 | if ( omfWriter_LEDataEnsureSpace(pThis, 1)
|
---|
913 | && omfWriter_RecAddU8(pThis, b))
|
---|
914 | {
|
---|
915 | pThis->offSeg += 1;
|
---|
916 | return true;
|
---|
917 | }
|
---|
918 | return false;
|
---|
919 | }
|
---|
920 |
|
---|
921 | /**
|
---|
922 | * MODEND - End of module, simple variant.
|
---|
923 | */
|
---|
924 | static bool omfWriter_EndModule(POMFWRITER pThis)
|
---|
925 | {
|
---|
926 | return omfWriter_RecBegin(pThis, OMF_MODEND32)
|
---|
927 | && omfWriter_RecAddU8(pThis, 0)
|
---|
928 | && omfWriter_RecEndWithCrc(pThis);
|
---|
929 | }
|
---|
930 |
|
---|
931 |
|
---|
932 |
|
---|
933 |
|
---|
934 | /*********************************************************************************************************************************
|
---|
935 | * ELF64/AMD64 -> ELF64/i386 Converter *
|
---|
936 | *********************************************************************************************************************************/
|
---|
937 |
|
---|
938 | /** AMD64 relocation type names for ELF. */
|
---|
939 | static const char * const g_apszElfAmd64RelTypes[] =
|
---|
940 | {
|
---|
941 | "R_X86_64_NONE",
|
---|
942 | "R_X86_64_64",
|
---|
943 | "R_X86_64_PC32",
|
---|
944 | "R_X86_64_GOT32",
|
---|
945 | "R_X86_64_PLT32",
|
---|
946 | "R_X86_64_COPY",
|
---|
947 | "R_X86_64_GLOB_DAT",
|
---|
948 | "R_X86_64_JMP_SLOT",
|
---|
949 | "R_X86_64_RELATIVE",
|
---|
950 | "R_X86_64_GOTPCREL",
|
---|
951 | "R_X86_64_32",
|
---|
952 | "R_X86_64_32S",
|
---|
953 | "R_X86_64_16",
|
---|
954 | "R_X86_64_PC16",
|
---|
955 | "R_X86_64_8",
|
---|
956 | "R_X86_64_PC8",
|
---|
957 | "R_X86_64_DTPMOD64",
|
---|
958 | "R_X86_64_DTPOFF64",
|
---|
959 | "R_X86_64_TPOFF64",
|
---|
960 | "R_X86_64_TLSGD",
|
---|
961 | "R_X86_64_TLSLD",
|
---|
962 | "R_X86_64_DTPOFF32",
|
---|
963 | "R_X86_64_GOTTPOFF",
|
---|
964 | "R_X86_64_TPOFF32",
|
---|
965 | };
|
---|
966 |
|
---|
967 | /** AMD64 relocation type sizes for ELF. */
|
---|
968 | static uint8_t const g_acbElfAmd64RelTypes[] =
|
---|
969 | {
|
---|
970 | 0, /* R_X86_64_NONE */
|
---|
971 | 8, /* R_X86_64_64 */
|
---|
972 | 4, /* R_X86_64_PC32 */
|
---|
973 | 4, /* R_X86_64_GOT32 */
|
---|
974 | 4, /* R_X86_64_PLT32 */
|
---|
975 | 0, /* R_X86_64_COPY */
|
---|
976 | 0, /* R_X86_64_GLOB_DAT */
|
---|
977 | 0, /* R_X86_64_JMP_SLOT */
|
---|
978 | 0, /* R_X86_64_RELATIVE */
|
---|
979 | 0, /* R_X86_64_GOTPCREL */
|
---|
980 | 4, /* R_X86_64_32 */
|
---|
981 | 4, /* R_X86_64_32S */
|
---|
982 | 2, /* R_X86_64_16 */
|
---|
983 | 2, /* R_X86_64_PC16 */
|
---|
984 | 1, /* R_X86_64_8 */
|
---|
985 | 1, /* R_X86_64_PC8 */
|
---|
986 | 0, /* R_X86_64_DTPMOD64 */
|
---|
987 | 0, /* R_X86_64_DTPOFF64 */
|
---|
988 | 0, /* R_X86_64_TPOFF64 */
|
---|
989 | 0, /* R_X86_64_TLSGD */
|
---|
990 | 0, /* R_X86_64_TLSLD */
|
---|
991 | 0, /* R_X86_64_DTPOFF32 */
|
---|
992 | 0, /* R_X86_64_GOTTPOFF */
|
---|
993 | 0, /* R_X86_64_TPOFF32 */
|
---|
994 | };
|
---|
995 |
|
---|
996 | /** Macro for getting the size of a AMD64 ELF relocation. */
|
---|
997 | #define ELF_AMD64_RELOC_SIZE(a_Type) ( (a_Type) < RT_ELEMENTS(g_acbElfAmd64RelTypes) ? g_acbElfAmd64RelTypes[(a_Type)] : 1)
|
---|
998 |
|
---|
999 |
|
---|
1000 | typedef struct ELFDETAILS
|
---|
1001 | {
|
---|
1002 | /** The ELF header. */
|
---|
1003 | Elf64_Ehdr const *pEhdr;
|
---|
1004 | /** The section header table. */
|
---|
1005 | Elf64_Shdr const *paShdrs;
|
---|
1006 | /** The string table for the section names. */
|
---|
1007 | const char *pchShStrTab;
|
---|
1008 |
|
---|
1009 | /** The symbol table section number. UINT16_MAX if not found. */
|
---|
1010 | uint16_t iSymSh;
|
---|
1011 | /** The string table section number. UINT16_MAX if not found. */
|
---|
1012 | uint16_t iStrSh;
|
---|
1013 |
|
---|
1014 | /** The symbol table. */
|
---|
1015 | Elf64_Sym const *paSymbols;
|
---|
1016 | /** The number of symbols in the symbol table. */
|
---|
1017 | uint32_t cSymbols;
|
---|
1018 |
|
---|
1019 | /** Pointer to the (symbol) string table if found. */
|
---|
1020 | const char *pchStrTab;
|
---|
1021 | /** The string table size. */
|
---|
1022 | size_t cbStrTab;
|
---|
1023 |
|
---|
1024 | } ELFDETAILS;
|
---|
1025 | typedef ELFDETAILS *PELFDETAILS;
|
---|
1026 | typedef ELFDETAILS const *PCELFDETAILS;
|
---|
1027 |
|
---|
1028 |
|
---|
1029 | static bool validateElf(const char *pszFile, uint8_t const *pbFile, size_t cbFile, PELFDETAILS pElfStuff)
|
---|
1030 | {
|
---|
1031 | /*
|
---|
1032 | * Initialize the ELF details structure.
|
---|
1033 | */
|
---|
1034 | memset(pElfStuff, 0, sizeof(*pElfStuff));
|
---|
1035 | pElfStuff->iSymSh = UINT16_MAX;
|
---|
1036 | pElfStuff->iStrSh = UINT16_MAX;
|
---|
1037 |
|
---|
1038 | /*
|
---|
1039 | * Validate the header and our other expectations.
|
---|
1040 | */
|
---|
1041 | Elf64_Ehdr const *pEhdr = (Elf64_Ehdr const *)pbFile;
|
---|
1042 | pElfStuff->pEhdr = pEhdr;
|
---|
1043 | if ( pEhdr->e_ident[EI_CLASS] != ELFCLASS64
|
---|
1044 | || pEhdr->e_ident[EI_DATA] != ELFDATA2LSB
|
---|
1045 | || pEhdr->e_ehsize != sizeof(Elf64_Ehdr)
|
---|
1046 | || pEhdr->e_shentsize != sizeof(Elf64_Shdr)
|
---|
1047 | || pEhdr->e_version != EV_CURRENT )
|
---|
1048 | return error(pszFile, "Unsupported ELF config\n");
|
---|
1049 | if (pEhdr->e_type != ET_REL)
|
---|
1050 | return error(pszFile, "Expected relocatable ELF file (e_type=%d)\n", pEhdr->e_type);
|
---|
1051 | if (pEhdr->e_machine != EM_X86_64)
|
---|
1052 | return error(pszFile, "Expected relocatable ELF file (e_type=%d)\n", pEhdr->e_machine);
|
---|
1053 | if (pEhdr->e_phnum != 0)
|
---|
1054 | return error(pszFile, "Expected e_phnum to be zero not %u\n", pEhdr->e_phnum);
|
---|
1055 | if (pEhdr->e_shnum < 2)
|
---|
1056 | return error(pszFile, "Expected e_shnum to be two or higher\n");
|
---|
1057 | if (pEhdr->e_shstrndx >= pEhdr->e_shnum || pEhdr->e_shstrndx == 0)
|
---|
1058 | return error(pszFile, "Bad e_shstrndx=%u (e_shnum=%u)\n", pEhdr->e_shstrndx, pEhdr->e_shnum);
|
---|
1059 | if ( pEhdr->e_shoff >= cbFile
|
---|
1060 | || pEhdr->e_shoff + pEhdr->e_shnum * sizeof(Elf64_Shdr) > cbFile)
|
---|
1061 | return error(pszFile, "Section table is outside the file (e_shoff=%#llx, e_shnum=%u, cbFile=%#llx)\n",
|
---|
1062 | pEhdr->e_shstrndx, pEhdr->e_shnum, (uint64_t)cbFile);
|
---|
1063 |
|
---|
1064 | /*
|
---|
1065 | * Locate the section name string table.
|
---|
1066 | * We assume it's okay as we only reference it in verbose mode.
|
---|
1067 | */
|
---|
1068 | Elf64_Shdr const *paShdrs = (Elf64_Shdr const *)&pbFile[pEhdr->e_shoff];
|
---|
1069 | pElfStuff->paShdrs = paShdrs;
|
---|
1070 |
|
---|
1071 | Elf64_Xword const cbShStrTab = paShdrs[pEhdr->e_shstrndx].sh_size;
|
---|
1072 | if ( paShdrs[pEhdr->e_shstrndx].sh_offset > cbFile
|
---|
1073 | || cbShStrTab > cbFile
|
---|
1074 | || paShdrs[pEhdr->e_shstrndx].sh_offset + cbShStrTab > cbFile)
|
---|
1075 | return error(pszFile,
|
---|
1076 | "Section string table is outside the file (sh_offset=%#" ELF_FMT_X64 " sh_size=%#" ELF_FMT_X64 " cbFile=%#" ELF_FMT_X64 ")\n",
|
---|
1077 | paShdrs[pEhdr->e_shstrndx].sh_offset, paShdrs[pEhdr->e_shstrndx].sh_size, (Elf64_Xword)cbFile);
|
---|
1078 | const char *pchShStrTab = (const char *)&pbFile[paShdrs[pEhdr->e_shstrndx].sh_offset];
|
---|
1079 | pElfStuff->pchShStrTab = pchShStrTab;
|
---|
1080 |
|
---|
1081 | /*
|
---|
1082 | * Work the section table.
|
---|
1083 | */
|
---|
1084 | bool fRet = true;
|
---|
1085 | for (uint32_t i = 1; i < pEhdr->e_shnum; i++)
|
---|
1086 | {
|
---|
1087 | if (paShdrs[i].sh_name >= cbShStrTab)
|
---|
1088 | return error(pszFile, "Invalid sh_name value (%#x) for section #%u\n", paShdrs[i].sh_name, i);
|
---|
1089 | const char *pszShNm = &pchShStrTab[paShdrs[i].sh_name];
|
---|
1090 |
|
---|
1091 | if ( paShdrs[i].sh_offset > cbFile
|
---|
1092 | || paShdrs[i].sh_size > cbFile
|
---|
1093 | || paShdrs[i].sh_offset + paShdrs[i].sh_size > cbFile)
|
---|
1094 | return error(pszFile, "Section #%u '%s' has data outside the file: %#" ELF_FMT_X64 " LB %#" ELF_FMT_X64 " (cbFile=%#" ELF_FMT_X64 ")\n",
|
---|
1095 | i, pszShNm, paShdrs[i].sh_offset, paShdrs[i].sh_size, (Elf64_Xword)cbFile);
|
---|
1096 | if (g_cVerbose)
|
---|
1097 | printf("shdr[%u]: name=%#x '%s' type=%#x flags=%#" ELF_FMT_X64 " addr=%#" ELF_FMT_X64 " off=%#" ELF_FMT_X64 " size=%#" ELF_FMT_X64 "\n"
|
---|
1098 | " link=%u info=%#x align=%#" ELF_FMT_X64 " entsize=%#" ELF_FMT_X64 "\n",
|
---|
1099 | i, paShdrs[i].sh_name, pszShNm, paShdrs[i].sh_type, paShdrs[i].sh_flags,
|
---|
1100 | paShdrs[i].sh_addr, paShdrs[i].sh_offset, paShdrs[i].sh_size,
|
---|
1101 | paShdrs[i].sh_link, paShdrs[i].sh_info, paShdrs[i].sh_addralign, paShdrs[i].sh_entsize);
|
---|
1102 |
|
---|
1103 | if (paShdrs[i].sh_link >= pEhdr->e_shnum)
|
---|
1104 | return error(pszFile, "Section #%u '%s' links to a section outside the section table: %#x, max %#x\n",
|
---|
1105 | i, pszShNm, paShdrs[i].sh_link, pEhdr->e_shnum);
|
---|
1106 | if (!RT_IS_POWER_OF_TWO(paShdrs[i].sh_addralign))
|
---|
1107 | return error(pszFile, "Section #%u '%s' alignment value is not a power of two: %#" ELF_FMT_X64 "\n",
|
---|
1108 | i, pszShNm, paShdrs[i].sh_addralign);
|
---|
1109 | if (!RT_IS_POWER_OF_TWO(paShdrs[i].sh_addralign))
|
---|
1110 | return error(pszFile, "Section #%u '%s' alignment value is not a power of two: %#" ELF_FMT_X64 "\n",
|
---|
1111 | i, pszShNm, paShdrs[i].sh_addralign);
|
---|
1112 | if (paShdrs[i].sh_addr != 0)
|
---|
1113 | return error(pszFile, "Section #%u '%s' has non-zero address: %#" ELF_FMT_X64 "\n", i, pszShNm, paShdrs[i].sh_addr);
|
---|
1114 |
|
---|
1115 | if (paShdrs[i].sh_type == SHT_RELA)
|
---|
1116 | {
|
---|
1117 | if (paShdrs[i].sh_entsize != sizeof(Elf64_Rela))
|
---|
1118 | return error(pszFile, "Expected sh_entsize to be %u not %u for section #%u (%s)\n", (unsigned)sizeof(Elf64_Rela),
|
---|
1119 | paShdrs[i].sh_entsize, i, pszShNm);
|
---|
1120 | uint32_t const cRelocs = paShdrs[i].sh_size / sizeof(Elf64_Rela);
|
---|
1121 | if (cRelocs * sizeof(Elf64_Rela) != paShdrs[i].sh_size)
|
---|
1122 | return error(pszFile, "Uneven relocation entry count in #%u (%s): sh_size=%#" ELF_FMT_X64 "\n",
|
---|
1123 | i, pszShNm, paShdrs[i].sh_size);
|
---|
1124 | if ( paShdrs[i].sh_offset > cbFile
|
---|
1125 | || paShdrs[i].sh_size >= cbFile
|
---|
1126 | || paShdrs[i].sh_offset + paShdrs[i].sh_size > cbFile)
|
---|
1127 | return error(pszFile, "The content of section #%u '%s' is outside the file (%#" ELF_FMT_X64 " LB %#" ELF_FMT_X64 ", cbFile=%#lx)\n",
|
---|
1128 | i, pszShNm, paShdrs[i].sh_offset, paShdrs[i].sh_size, (unsigned long)cbFile);
|
---|
1129 | if (paShdrs[i].sh_info != i - 1)
|
---|
1130 | return error(pszFile, "Expected relocation section #%u (%s) to link to previous section: sh_info=%#u\n",
|
---|
1131 | i, pszShNm, (unsigned)paShdrs[i].sh_link);
|
---|
1132 | if (paShdrs[paShdrs[i].sh_link].sh_type != SHT_SYMTAB)
|
---|
1133 | return error(pszFile, "Expected relocation section #%u (%s) to link to symbol table: sh_link=%#u -> sh_type=%#x\n",
|
---|
1134 | i, pszShNm, (unsigned)paShdrs[i].sh_link, (unsigned)paShdrs[paShdrs[i].sh_link].sh_type);
|
---|
1135 | uint32_t cSymbols = paShdrs[paShdrs[i].sh_link].sh_size / paShdrs[paShdrs[i].sh_link].sh_entsize;
|
---|
1136 |
|
---|
1137 | Elf64_Rela const *paRelocs = (Elf64_Rela *)&pbFile[paShdrs[i].sh_offset];
|
---|
1138 | for (uint32_t j = 0; j < cRelocs; j++)
|
---|
1139 | {
|
---|
1140 | uint8_t const bType = ELF64_R_TYPE(paRelocs[j].r_info);
|
---|
1141 | if (RT_UNLIKELY(bType >= R_X86_64_COUNT))
|
---|
1142 | fRet = error(pszFile,
|
---|
1143 | "%#018" ELF_FMT_X64 " %#018" ELF_FMT_X64 ": unknown fix up %#x (%+" ELF_FMT_D64 ")\n",
|
---|
1144 | paRelocs[j].r_offset, paRelocs[j].r_info, bType, paRelocs[j].r_addend);
|
---|
1145 | if (RT_UNLIKELY( paRelocs[j].r_offset > paShdrs[i - 1].sh_size
|
---|
1146 | || paRelocs[j].r_offset + ELF_AMD64_RELOC_SIZE(ELF64_R_TYPE(paRelocs[j].r_info))
|
---|
1147 | > paShdrs[i - 1].sh_size))
|
---|
1148 | fRet = error(pszFile,
|
---|
1149 | "%#018" ELF_FMT_X64 " %#018" ELF_FMT_X64 ": out of bounds (sh_size %" ELF_FMT_X64 ")\n",
|
---|
1150 | paRelocs[j].r_offset, paRelocs[j].r_info, paShdrs[i - 1].sh_size);
|
---|
1151 |
|
---|
1152 | uint32_t const iSymbol = ELF64_R_SYM(paRelocs[j].r_info);
|
---|
1153 | if (RT_UNLIKELY(iSymbol >= cSymbols))
|
---|
1154 | fRet = error(pszFile,
|
---|
1155 | "%#018" ELF_FMT_X64 " %#018" ELF_FMT_X64 ": symbol index (%#x) out of bounds (%#x)\n",
|
---|
1156 | paRelocs[j].r_offset, paRelocs[j].r_info, iSymbol, cSymbols);
|
---|
1157 | }
|
---|
1158 | }
|
---|
1159 | else if (paShdrs[i].sh_type == SHT_REL)
|
---|
1160 | fRet = error(pszFile, "Section #%u '%s': Unexpected SHT_REL section\n", i, pszShNm);
|
---|
1161 | else if (paShdrs[i].sh_type == SHT_SYMTAB)
|
---|
1162 | {
|
---|
1163 | if (paShdrs[i].sh_entsize != sizeof(Elf64_Sym))
|
---|
1164 | fRet = error(pszFile, "Section #%u '%s': Unsupported symbol table entry size in : #%u (expected #%u)\n",
|
---|
1165 | i, pszShNm, paShdrs[i].sh_entsize, sizeof(Elf64_Sym));
|
---|
1166 | Elf64_Xword const cSymbols = paShdrs[i].sh_size / paShdrs[i].sh_entsize;
|
---|
1167 | if (cSymbols * paShdrs[i].sh_entsize != paShdrs[i].sh_size)
|
---|
1168 | fRet = error(pszFile, "Section #%u '%s': Size not a multiple of entry size: %#" ELF_FMT_X64 " %% %#" ELF_FMT_X64 " = %#" ELF_FMT_X64 "\n",
|
---|
1169 | i, pszShNm, paShdrs[i].sh_size, paShdrs[i].sh_entsize, paShdrs[i].sh_size % paShdrs[i].sh_entsize);
|
---|
1170 | if (cSymbols > UINT32_MAX)
|
---|
1171 | fRet = error(pszFile, "Section #%u '%s': too many symbols: %" ELF_FMT_X64 "\n",
|
---|
1172 | i, pszShNm, paShdrs[i].sh_size, cSymbols);
|
---|
1173 |
|
---|
1174 | if (pElfStuff->iSymSh == UINT16_MAX)
|
---|
1175 | {
|
---|
1176 | pElfStuff->iSymSh = (uint16_t)i;
|
---|
1177 | pElfStuff->paSymbols = (Elf64_Sym const *)&pbFile[paShdrs[i].sh_offset];
|
---|
1178 | pElfStuff->cSymbols = cSymbols;
|
---|
1179 |
|
---|
1180 | if (paShdrs[i].sh_link != 0)
|
---|
1181 | {
|
---|
1182 | /* Note! The symbol string table section header may not have been validated yet! */
|
---|
1183 | Elf64_Shdr const *pStrTabShdr = &paShdrs[paShdrs[i].sh_link];
|
---|
1184 | pElfStuff->iStrSh = paShdrs[i].sh_link;
|
---|
1185 | pElfStuff->pchStrTab = (const char *)&pbFile[pStrTabShdr->sh_offset];
|
---|
1186 | pElfStuff->cbStrTab = (size_t)pStrTabShdr->sh_size;
|
---|
1187 | }
|
---|
1188 | else
|
---|
1189 | fRet = error(pszFile, "Section #%u '%s': String table link is out of bounds (%#x)\n",
|
---|
1190 | i, pszShNm, paShdrs[i].sh_link);
|
---|
1191 | }
|
---|
1192 | else
|
---|
1193 | fRet = error(pszFile, "Section #%u '%s': Found additonal symbol table, previous in #%u\n",
|
---|
1194 | i, pszShNm, pElfStuff->iSymSh);
|
---|
1195 | }
|
---|
1196 | }
|
---|
1197 | return fRet;
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 |
|
---|
1201 | static bool convertElfSectionsToSegDefsAndGrpDefs(POMFWRITER pThis, PCELFDETAILS pElfStuff)
|
---|
1202 | {
|
---|
1203 | /*
|
---|
1204 | * Do the list of names pass.
|
---|
1205 | */
|
---|
1206 | uint16_t idxGrpFlat, idxGrpData;
|
---|
1207 | uint16_t idxClassCode, idxClassData, idxClassDwarf;
|
---|
1208 | if ( !omfWriter_LNamesBegin(pThis, true /*fAddZeroEntry*/)
|
---|
1209 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("FLAT"), &idxGrpFlat)
|
---|
1210 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("BS3DATA64_GROUP"), &idxGrpData)
|
---|
1211 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("BS3CLASS64CODE"), &idxClassCode)
|
---|
1212 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("FAR_DATA"), &idxClassData)
|
---|
1213 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("DWARF"), &idxClassDwarf)
|
---|
1214 | )
|
---|
1215 | return false;
|
---|
1216 |
|
---|
1217 | bool fHaveData = false;
|
---|
1218 | Elf64_Shdr const *pShdr = &pElfStuff->paShdrs[1];
|
---|
1219 | Elf64_Half const cSections = pElfStuff->pEhdr->e_shnum;
|
---|
1220 | for (Elf64_Half i = 1; i < cSections; i++, pShdr++)
|
---|
1221 | {
|
---|
1222 | const char *pszName = &pElfStuff->pchShStrTab[pShdr->sh_name];
|
---|
1223 | if (*pszName == '\0')
|
---|
1224 | return error(pThis->pszSrc, "Section #%u has an empty name!\n", i);
|
---|
1225 |
|
---|
1226 | switch (pShdr->sh_type)
|
---|
1227 | {
|
---|
1228 | case SHT_PROGBITS:
|
---|
1229 | case SHT_NOBITS:
|
---|
1230 | /* We drop a few sections we don't want:. */
|
---|
1231 | if ( strcmp(pszName, ".comment") != 0 /* compiler info */
|
---|
1232 | && strcmp(pszName, ".note.GNU-stack") != 0 /* some empty section for hinting the linker/whatever */
|
---|
1233 | && strcmp(pszName, ".eh_frame") != 0 /* unwind / exception info */
|
---|
1234 | )
|
---|
1235 | {
|
---|
1236 | pThis->paSegments[i].iSegDef = UINT16_MAX;
|
---|
1237 | pThis->paSegments[i].iGrpDef = UINT16_MAX;
|
---|
1238 |
|
---|
1239 | /* Translate the name and determine group and class.
|
---|
1240 | Note! We currently strip sub-sections. */
|
---|
1241 | if ( strcmp(pszName, ".text") == 0
|
---|
1242 | || strncmp(pszName, RT_STR_TUPLE(".text.")) == 0)
|
---|
1243 | {
|
---|
1244 | pszName = "BS3TEXT64";
|
---|
1245 | pThis->paSegments[i].iGrpNm = idxGrpFlat;
|
---|
1246 | pThis->paSegments[i].iClassNm = idxClassCode;
|
---|
1247 | }
|
---|
1248 | else if ( strcmp(pszName, ".data") == 0
|
---|
1249 | || strncmp(pszName, RT_STR_TUPLE(".data.")) == 0)
|
---|
1250 | {
|
---|
1251 | pszName = "BS3DATA64";
|
---|
1252 | pThis->paSegments[i].iGrpNm = idxGrpData;
|
---|
1253 | pThis->paSegments[i].iClassNm = idxClassData;
|
---|
1254 | }
|
---|
1255 | else if (strcmp(pszName, ".bss") == 0)
|
---|
1256 | {
|
---|
1257 | pszName = "BS3BSS64";
|
---|
1258 | pThis->paSegments[i].iGrpNm = idxGrpData;
|
---|
1259 | pThis->paSegments[i].iClassNm = idxClassData;
|
---|
1260 | }
|
---|
1261 | else if ( strcmp(pszName, ".rodata") == 0
|
---|
1262 | || strncmp(pszName, RT_STR_TUPLE(".rodata.")) == 0)
|
---|
1263 | {
|
---|
1264 | pszName = "BS3DATA64CONST";
|
---|
1265 | pThis->paSegments[i].iGrpNm = idxGrpData;
|
---|
1266 | pThis->paSegments[i].iClassNm = idxClassData;
|
---|
1267 | }
|
---|
1268 | else if (strncmp(pszName, RT_STR_TUPLE(".debug_")) == 0)
|
---|
1269 | {
|
---|
1270 | pThis->paSegments[i].iGrpNm = UINT16_MAX;
|
---|
1271 | pThis->paSegments[i].iClassNm = idxClassDwarf;
|
---|
1272 | }
|
---|
1273 | else
|
---|
1274 | {
|
---|
1275 | pThis->paSegments[i].iGrpNm = idxGrpData;
|
---|
1276 | pThis->paSegments[i].iClassNm = idxClassData;
|
---|
1277 | error(pThis->pszSrc, "Unknown data (?) segment: '%s'\n", pszName);
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 | /* Save the name. */
|
---|
1281 | pThis->paSegments[i].pszName = strdup(pszName);
|
---|
1282 | if (!pThis->paSegments[i].pszName)
|
---|
1283 | return error(pThis->pszSrc, "Out of memory!\n");
|
---|
1284 |
|
---|
1285 | /* Add the section name. */
|
---|
1286 | if (!omfWriter_LNamesAdd(pThis, pThis->paSegments[i].pszName, &pThis->paSegments[i].iSegNm))
|
---|
1287 | return false;
|
---|
1288 |
|
---|
1289 | fHaveData |= pThis->paSegments[i].iGrpNm == idxGrpData;
|
---|
1290 | break;
|
---|
1291 | }
|
---|
1292 | /* fall thru */
|
---|
1293 |
|
---|
1294 | default:
|
---|
1295 | pThis->paSegments[i].iSegDef = UINT16_MAX;
|
---|
1296 | pThis->paSegments[i].iGrpDef = UINT16_MAX;
|
---|
1297 | pThis->paSegments[i].iSegNm = UINT16_MAX;
|
---|
1298 | pThis->paSegments[i].iGrpNm = UINT16_MAX;
|
---|
1299 | pThis->paSegments[i].iClassNm = UINT16_MAX;
|
---|
1300 | pThis->paSegments[i].pszName = NULL;
|
---|
1301 | break;
|
---|
1302 | }
|
---|
1303 | }
|
---|
1304 |
|
---|
1305 | if (!omfWriter_LNamesEnd(pThis))
|
---|
1306 | return false;
|
---|
1307 |
|
---|
1308 | /*
|
---|
1309 | * Emit segment definitions.
|
---|
1310 | */
|
---|
1311 | uint16_t iSegDef = 1; /* Start counting at 1. */
|
---|
1312 | pShdr = &pElfStuff->paShdrs[1];
|
---|
1313 | for (Elf64_Half i = 1; i < cSections; i++, pShdr++)
|
---|
1314 | {
|
---|
1315 | if (pThis->paSegments[i].iSegNm == UINT16_MAX)
|
---|
1316 | continue;
|
---|
1317 |
|
---|
1318 | uint8_t bSegAttr = 0;
|
---|
1319 |
|
---|
1320 | /* The A field. */
|
---|
1321 | switch (pShdr->sh_addralign)
|
---|
1322 | {
|
---|
1323 | case 0:
|
---|
1324 | case 1:
|
---|
1325 | bSegAttr |= 1 << 5;
|
---|
1326 | break;
|
---|
1327 | case 2:
|
---|
1328 | bSegAttr |= 2 << 5;
|
---|
1329 | break;
|
---|
1330 | case 4:
|
---|
1331 | bSegAttr |= 5 << 5;
|
---|
1332 | break;
|
---|
1333 | case 8:
|
---|
1334 | case 16:
|
---|
1335 | bSegAttr |= 3 << 5;
|
---|
1336 | break;
|
---|
1337 | case 32:
|
---|
1338 | case 64:
|
---|
1339 | case 128:
|
---|
1340 | case 256:
|
---|
1341 | bSegAttr |= 4 << 5;
|
---|
1342 | break;
|
---|
1343 | default:
|
---|
1344 | bSegAttr |= 6 << 5; /* page aligned, pharlabs extension. */
|
---|
1345 | break;
|
---|
1346 | }
|
---|
1347 |
|
---|
1348 | /* The C field. */
|
---|
1349 | bSegAttr |= 2 << 2; /* public */
|
---|
1350 |
|
---|
1351 | /* The B field. We don't have 4GB segments, so leave it as zero. */
|
---|
1352 |
|
---|
1353 | /* The D field shall be set as we're doing USE32. */
|
---|
1354 | bSegAttr |= 1;
|
---|
1355 |
|
---|
1356 |
|
---|
1357 | /* Done. */
|
---|
1358 | if (!omfWriter_SegDef(pThis, bSegAttr, (uint32_t)pShdr->sh_size,
|
---|
1359 | pThis->paSegments[i].iSegNm,
|
---|
1360 | pThis->paSegments[i].iClassNm))
|
---|
1361 | return false;
|
---|
1362 | pThis->paSegments[i].iSegDef = iSegDef++;
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | /*
|
---|
1366 | * Flat group definition (#1) - special, no members.
|
---|
1367 | */
|
---|
1368 | uint16_t iGrpDef = 1;
|
---|
1369 | if ( !omfWriter_GrpDefBegin(pThis, idxGrpFlat)
|
---|
1370 | || !omfWriter_GrpDefEnd(pThis))
|
---|
1371 | return false;
|
---|
1372 | for (uint16_t i = 0; i < cSections; i++)
|
---|
1373 | if (pThis->paSegments[i].iGrpNm == idxGrpFlat)
|
---|
1374 | pThis->paSegments[i].iGrpDef = iGrpDef;
|
---|
1375 | pThis->idxGrpFlat = iGrpDef++;
|
---|
1376 |
|
---|
1377 | /*
|
---|
1378 | * Data group definition (#2).
|
---|
1379 | */
|
---|
1380 | /** @todo do we need to consider missing segments and ordering? */
|
---|
1381 | uint16_t cGrpNms = 0;
|
---|
1382 | uint16_t aiGrpNms[2];
|
---|
1383 | if (fHaveData)
|
---|
1384 | aiGrpNms[cGrpNms++] = idxGrpData;
|
---|
1385 | for (uint32_t iGrpNm = 0; iGrpNm < cGrpNms; iGrpNm++)
|
---|
1386 | {
|
---|
1387 | if (!omfWriter_GrpDefBegin(pThis, aiGrpNms[iGrpNm]))
|
---|
1388 | return false;
|
---|
1389 | for (uint16_t i = 0; i < cSections; i++)
|
---|
1390 | if (pThis->paSegments[i].iGrpNm == aiGrpNms[iGrpNm])
|
---|
1391 | {
|
---|
1392 | pThis->paSegments[i].iGrpDef = iGrpDef;
|
---|
1393 | if (!omfWriter_GrpDefAddSegDef(pThis, pThis->paSegments[i].iSegDef))
|
---|
1394 | return false;
|
---|
1395 | }
|
---|
1396 | if (!omfWriter_GrpDefEnd(pThis))
|
---|
1397 | return false;
|
---|
1398 | iGrpDef++;
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | return true;
|
---|
1402 | }
|
---|
1403 |
|
---|
1404 | static bool convertElfSymbolsToPubDefsAndExtDefs(POMFWRITER pThis, PCELFDETAILS pElfStuff)
|
---|
1405 | {
|
---|
1406 | if (!pElfStuff->cSymbols)
|
---|
1407 | return true;
|
---|
1408 |
|
---|
1409 | /*
|
---|
1410 | * Process the symbols the first.
|
---|
1411 | */
|
---|
1412 | uint32_t cAbsSyms = 0;
|
---|
1413 | uint32_t cExtSyms = 0;
|
---|
1414 | uint32_t cPubSyms = 0;
|
---|
1415 | for (uint32_t iSeg = 0; iSeg < pThis->cSegments; iSeg++)
|
---|
1416 | pThis->paSegments[iSeg].cPubDefs = 0;
|
---|
1417 |
|
---|
1418 | uint32_t const cSections = pElfStuff->pEhdr->e_shnum;
|
---|
1419 | uint32_t const cSymbols = pElfStuff->cSymbols;
|
---|
1420 | Elf64_Sym const * const paSymbols = pElfStuff->paSymbols;
|
---|
1421 | for (uint32_t iSym = 0; iSym < cSymbols; iSym++)
|
---|
1422 | {
|
---|
1423 | const uint8_t bBind = ELF64_ST_BIND(paSymbols[iSym].st_info);
|
---|
1424 | const uint8_t bType = ELF64_ST_TYPE(paSymbols[iSym].st_info);
|
---|
1425 | const char *pszSymName = &pElfStuff->pchStrTab[paSymbols[iSym].st_name];
|
---|
1426 | if ( *pszSymName == '\0'
|
---|
1427 | && bType == STT_SECTION
|
---|
1428 | && paSymbols[iSym].st_shndx < cSections)
|
---|
1429 | pszSymName = &pElfStuff->pchShStrTab[pElfStuff->paShdrs[paSymbols[iSym].st_shndx].sh_name];
|
---|
1430 |
|
---|
1431 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_IGNORED;
|
---|
1432 | pThis->paSymbols[iSym].idx = UINT16_MAX;
|
---|
1433 | pThis->paSymbols[iSym].idxSegDef = UINT16_MAX;
|
---|
1434 | pThis->paSymbols[iSym].idxGrpDef = UINT16_MAX;
|
---|
1435 |
|
---|
1436 | uint32_t const idxSection = paSymbols[iSym].st_shndx;
|
---|
1437 | if (idxSection == SHN_UNDEF)
|
---|
1438 | {
|
---|
1439 | if (bBind == STB_GLOBAL)
|
---|
1440 | {
|
---|
1441 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_EXTDEF;
|
---|
1442 | cExtSyms++;
|
---|
1443 | if (*pszSymName == '\0')
|
---|
1444 | return error(pThis->pszSrc, "External symbol #%u (%s) has an empty name.\n", iSym, pszSymName);
|
---|
1445 | }
|
---|
1446 | else if (bBind != STB_LOCAL || iSym != 0) /* Entry zero is usually a dummy. */
|
---|
1447 | return error(pThis->pszSrc, "Unsupported or invalid bind type %#x for undefined symbol #%u (%s)\n",
|
---|
1448 | bBind, iSym, pszSymName);
|
---|
1449 | }
|
---|
1450 | else if (idxSection < cSections)
|
---|
1451 | {
|
---|
1452 | pThis->paSymbols[iSym].idxSegDef = pThis->paSegments[idxSection].iSegDef;
|
---|
1453 | pThis->paSymbols[iSym].idxGrpDef = pThis->paSegments[idxSection].iGrpDef;
|
---|
1454 | if (bBind == STB_GLOBAL)
|
---|
1455 | {
|
---|
1456 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_PUBDEF;
|
---|
1457 | pThis->paSegments[idxSection].cPubDefs++;
|
---|
1458 | cPubSyms++;
|
---|
1459 | if (bType == STT_SECTION)
|
---|
1460 | return error(pThis->pszSrc, "Don't know how to export STT_SECTION symbol #%u (%s)\n", iSym, pszSymName);
|
---|
1461 | if (*pszSymName == '\0')
|
---|
1462 | return error(pThis->pszSrc, "Public symbol #%u (%s) has an empty name.\n", iSym, pszSymName);
|
---|
1463 | }
|
---|
1464 | else if (bType == STT_SECTION)
|
---|
1465 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_SEGDEF;
|
---|
1466 | else
|
---|
1467 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_INTERNAL;
|
---|
1468 | }
|
---|
1469 | else if (idxSection == SHN_ABS)
|
---|
1470 | {
|
---|
1471 | if (bType != STT_FILE)
|
---|
1472 | {
|
---|
1473 | if (bBind == STB_GLOBAL)
|
---|
1474 | {
|
---|
1475 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_PUBDEF;
|
---|
1476 | pThis->paSymbols[iSym].idxSegDef = 0;
|
---|
1477 | pThis->paSymbols[iSym].idxGrpDef = 0;
|
---|
1478 | cAbsSyms++;
|
---|
1479 | if (*pszSymName == '\0')
|
---|
1480 | return error(pThis->pszSrc, "Public absolute symbol #%u (%s) has an empty name.\n", iSym, pszSymName);
|
---|
1481 | }
|
---|
1482 | else
|
---|
1483 | return error(pThis->pszSrc, "Unsupported or invalid bind type %#x for absolute symbol #%u (%s)\n",
|
---|
1484 | bBind, iSym, pszSymName);
|
---|
1485 | }
|
---|
1486 | }
|
---|
1487 | else
|
---|
1488 | return error(pThis->pszSrc, "Unsupported or invalid section number %#x for symbol #%u (%s)\n",
|
---|
1489 | idxSection, iSym, pszSymName);
|
---|
1490 | }
|
---|
1491 |
|
---|
1492 | /*
|
---|
1493 | * Emit the PUBDEFs the first time around (see order of records in TIS spec).
|
---|
1494 | */
|
---|
1495 | uint16_t idxPubDef = 1;
|
---|
1496 | if (cPubSyms)
|
---|
1497 | {
|
---|
1498 | for (uint32_t iSeg = 0; iSeg < pThis->cSegments; iSeg++)
|
---|
1499 | if (pThis->paSegments[iSeg].cPubDefs > 0)
|
---|
1500 | {
|
---|
1501 | uint16_t const idxSegDef = pThis->paSegments[iSeg].iSegDef;
|
---|
1502 | if (!omfWriter_PubDefBegin(pThis, pThis->paSegments[iSeg].iGrpDef, idxSegDef))
|
---|
1503 | return false;
|
---|
1504 | for (uint16_t iSym = 0; iSym < cSymbols; iSym++)
|
---|
1505 | if ( pThis->paSymbols[iSym].idxSegDef == idxSegDef
|
---|
1506 | && pThis->paSymbols[iSym].enmType == OMFSYMTYPE_PUBDEF)
|
---|
1507 | {
|
---|
1508 | const char *pszName = &pElfStuff->pchStrTab[paSymbols[iSym].st_name];
|
---|
1509 | if (!omfWriter_PubDefAdd(pThis, paSymbols[iSym].st_value, pszName))
|
---|
1510 | return false;
|
---|
1511 |
|
---|
1512 | /* If the symbol doesn't start with an underscore and is a _c64 or _lm64 symbol,
|
---|
1513 | add an underscore prefixed alias to ease access from 16-bit and 32-bit code. */
|
---|
1514 | size_t cchName = strlen(pszName);
|
---|
1515 | if ( *pszName != '_'
|
---|
1516 | && ( (cchName > 4 && strcmp(&pszName[cchName - 4], "_c64") == 0)
|
---|
1517 | || (cchName > 5 && strcmp(&pszName[cchName - 5], "_lm64") == 0) ) )
|
---|
1518 | {
|
---|
1519 | char szCdeclName[512];
|
---|
1520 | if (cchName > sizeof(szCdeclName) - 2)
|
---|
1521 | cchName = sizeof(szCdeclName) - 2;
|
---|
1522 | szCdeclName[0] = '_';
|
---|
1523 | memcpy(&szCdeclName[1], pszName, cchName);
|
---|
1524 | szCdeclName[cchName + 1] = '\0';
|
---|
1525 | if (!omfWriter_PubDefAdd(pThis, paSymbols[iSym].st_value, szCdeclName))
|
---|
1526 | return false;
|
---|
1527 | }
|
---|
1528 |
|
---|
1529 | pThis->paSymbols[iSym].idx = idxPubDef++;
|
---|
1530 | }
|
---|
1531 | if (!omfWriter_PubDefEnd(pThis))
|
---|
1532 | return false;
|
---|
1533 | }
|
---|
1534 | }
|
---|
1535 |
|
---|
1536 | if (cAbsSyms > 0)
|
---|
1537 | {
|
---|
1538 | if (!omfWriter_PubDefBegin(pThis, 0, 0))
|
---|
1539 | return false;
|
---|
1540 | for (uint16_t iSym = 0; iSym < cSymbols; iSym++)
|
---|
1541 | if ( pThis->paSymbols[iSym].idxSegDef == 0
|
---|
1542 | && pThis->paSymbols[iSym].enmType == OMFSYMTYPE_PUBDEF)
|
---|
1543 | {
|
---|
1544 | const char *pszName = &pElfStuff->pchStrTab[paSymbols[iSym].st_name];
|
---|
1545 | if (!omfWriter_PubDefAdd(pThis, paSymbols[iSym].st_value, pszName))
|
---|
1546 | return false;
|
---|
1547 | pThis->paSymbols[iSym].idx = idxPubDef++;
|
---|
1548 | }
|
---|
1549 | if (!omfWriter_PubDefEnd(pThis))
|
---|
1550 | return false;
|
---|
1551 | }
|
---|
1552 |
|
---|
1553 | /*
|
---|
1554 | * Go over the symbol table and emit external definition records.
|
---|
1555 | */
|
---|
1556 | if (!omfWriter_ExtDefBegin(pThis))
|
---|
1557 | return false;
|
---|
1558 | uint16_t idxExtDef = 1;
|
---|
1559 | for (uint16_t iSym = 0; iSym < cSymbols; iSym++)
|
---|
1560 | if (pThis->paSymbols[iSym].enmType == OMFSYMTYPE_EXTDEF)
|
---|
1561 | {
|
---|
1562 | const char *pszName = &pElfStuff->pchStrTab[paSymbols[iSym].st_name];
|
---|
1563 | if (!omfWriter_ExtDefAdd(pThis, pszName))
|
---|
1564 | return false;
|
---|
1565 | pThis->paSymbols[iSym].idx = idxExtDef++;
|
---|
1566 | }
|
---|
1567 |
|
---|
1568 | if (!omfWriter_ExtDefEnd(pThis))
|
---|
1569 | return false;
|
---|
1570 |
|
---|
1571 | return true;
|
---|
1572 | }
|
---|
1573 |
|
---|
1574 | /**
|
---|
1575 | * @callback_method_impl{FNRTSORTCMP, For Elf64_Rela tables.}
|
---|
1576 | */
|
---|
1577 | static DECLCALLBACK(int) convertElfCompareRelA(void const *pvElement1, void const *pvElement2, void *pvUser)
|
---|
1578 | {
|
---|
1579 | Elf64_Rela const *pReloc1 = (Elf64_Rela const *)pvElement1;
|
---|
1580 | Elf64_Rela const *pReloc2 = (Elf64_Rela const *)pvElement2;
|
---|
1581 | if (pReloc1->r_offset < pReloc2->r_offset)
|
---|
1582 | return -1;
|
---|
1583 | if (pReloc1->r_offset > pReloc2->r_offset)
|
---|
1584 | return 1;
|
---|
1585 | return 0;
|
---|
1586 | }
|
---|
1587 |
|
---|
1588 | static bool convertElfSectionsToLeDataAndFixupps(POMFWRITER pThis, PCELFDETAILS pElfStuff, uint8_t const *pbFile, size_t cbFile)
|
---|
1589 | {
|
---|
1590 | Elf64_Sym const *paSymbols = pElfStuff->paSymbols;
|
---|
1591 | Elf64_Shdr const *paShdrs = pElfStuff->paShdrs;
|
---|
1592 | bool fRet = true;
|
---|
1593 | for (uint32_t i = 1; i < pThis->cSegments; i++)
|
---|
1594 | {
|
---|
1595 | if (pThis->paSegments[i].iSegDef == UINT16_MAX)
|
---|
1596 | continue;
|
---|
1597 |
|
---|
1598 | const char *pszSegNm = &pElfStuff->pchShStrTab[paShdrs[i].sh_name];
|
---|
1599 | bool const fRelocs = i + 1 < pThis->cSegments && paShdrs[i + 1].sh_type == SHT_RELA;
|
---|
1600 | uint32_t cRelocs = fRelocs ? paShdrs[i + 1].sh_size / sizeof(Elf64_Rela) : 0;
|
---|
1601 | Elf64_Rela const *paRelocs = fRelocs ? (Elf64_Rela *)&pbFile[paShdrs[i + 1].sh_offset] : NULL;
|
---|
1602 | Elf64_Xword cbVirtData = paShdrs[i].sh_size;
|
---|
1603 | Elf64_Xword cbData = paShdrs[i].sh_type == SHT_NOBITS ? 0 : cbVirtData;
|
---|
1604 | uint8_t const *pbData = &pbFile[paShdrs[i].sh_offset];
|
---|
1605 | uint32_t off = 0;
|
---|
1606 |
|
---|
1607 | /* We sort fixups by r_offset in order to more easily split them into chunks. */
|
---|
1608 | RTSortShell((void *)paRelocs, cRelocs, sizeof(paRelocs[0]), convertElfCompareRelA, NULL);
|
---|
1609 |
|
---|
1610 | /* The OMF record size requires us to split larger sections up. To make
|
---|
1611 | life simple, we fill zeros for unitialized (BSS) stuff. */
|
---|
1612 | const uint32_t cbMaxData = RT_MIN(OMF_MAX_RECORD_PAYLOAD - 1 - (pThis->paSegments[i].iSegDef >= 128) - 4 - 1, _1K);
|
---|
1613 | while (cbVirtData > 0)
|
---|
1614 | {
|
---|
1615 | /* Figure out how many bytes to put out in this chunk. Must make sure
|
---|
1616 | fixups doesn't cross chunk boundraries. ASSUMES sorted relocs. */
|
---|
1617 | uint32_t cChunkRelocs = cRelocs;
|
---|
1618 | uint32_t cbChunk = cbVirtData;
|
---|
1619 | uint32_t offEnd = off + cbChunk;
|
---|
1620 | if (cbChunk > cbMaxData)
|
---|
1621 | {
|
---|
1622 | cbChunk = cbMaxData;
|
---|
1623 | offEnd = off + cbChunk;
|
---|
1624 | cChunkRelocs = 0;
|
---|
1625 |
|
---|
1626 | /* Quickly determin the reloc range. */
|
---|
1627 | while ( cChunkRelocs < cRelocs
|
---|
1628 | && paRelocs[cChunkRelocs].r_offset < offEnd)
|
---|
1629 | cChunkRelocs++;
|
---|
1630 |
|
---|
1631 | /* Ensure final reloc doesn't go beyond chunk. */
|
---|
1632 | while ( cChunkRelocs > 0
|
---|
1633 | && paRelocs[cChunkRelocs - 1].r_offset
|
---|
1634 | + ELF_AMD64_RELOC_SIZE(ELF64_R_TYPE(paRelocs[cChunkRelocs - 1].r_info))
|
---|
1635 | > offEnd)
|
---|
1636 | {
|
---|
1637 | uint32_t cbDrop = offEnd - paRelocs[cChunkRelocs - 1].r_offset;
|
---|
1638 | cbChunk -= cbDrop;
|
---|
1639 | offEnd -= cbDrop;
|
---|
1640 | cChunkRelocs--;
|
---|
1641 | }
|
---|
1642 |
|
---|
1643 | if (!cbVirtData)
|
---|
1644 | return error(pThis->pszSrc, "Wtf? cbVirtData is zero!\n");
|
---|
1645 | }
|
---|
1646 | if (g_cVerbose >= 2)
|
---|
1647 | printf("debug: LEDATA off=%#x cb=%#x cRelocs=%#x sect=#%u segdef=%#x grpdef=%#x '%s'\n",
|
---|
1648 | off, cbChunk, cRelocs, i, pThis->paSegments[i].iSegDef, pThis->paSegments[i].iGrpDef, pszSegNm);
|
---|
1649 |
|
---|
1650 | /*
|
---|
1651 | * We stash the bytes into the OMF writer record buffer, receiving a
|
---|
1652 | * pointer to the start of it so we can make adjustments if necessary.
|
---|
1653 | */
|
---|
1654 | uint8_t *pbCopy;
|
---|
1655 | if (!omfWriter_LEDataBeginEx(pThis, pThis->paSegments[i].iSegDef, off, cbChunk, cbData, pbData, &pbCopy))
|
---|
1656 | return false;
|
---|
1657 |
|
---|
1658 | /*
|
---|
1659 | * Convert fiuxps.
|
---|
1660 | */
|
---|
1661 | for (uint32_t iReloc = 0; iReloc < cChunkRelocs; iReloc++)
|
---|
1662 | {
|
---|
1663 | /* Get the OMF and ELF data for the symbol the reloc references. */
|
---|
1664 | uint32_t const uType = ELF64_R_TYPE(paRelocs[iReloc].r_info);
|
---|
1665 | uint32_t const iSymbol = ELF64_R_SYM(paRelocs[iReloc].r_info);
|
---|
1666 | Elf64_Sym const * const pElfSym = &paSymbols[iSymbol];
|
---|
1667 | POMFSYMBOL const pOmfSym = &pThis->paSymbols[iSymbol];
|
---|
1668 | const char * const pszSymName = &pElfStuff->pchStrTab[pElfSym->st_name];
|
---|
1669 |
|
---|
1670 | /* Calc fixup location in the pending chunk and setup a flexible pointer to it. */
|
---|
1671 | uint16_t offDataRec = (uint16_t)(paRelocs[iReloc].r_offset - off);
|
---|
1672 | RTPTRUNION uLoc;
|
---|
1673 | uLoc.pu8 = &pbCopy[offDataRec];
|
---|
1674 |
|
---|
1675 | /* OMF fixup data initialized with typical defaults. */
|
---|
1676 | bool fSelfRel = true;
|
---|
1677 | uint8_t bLocation = OMF_FIX_LOC_32BIT_OFFSET;
|
---|
1678 | uint8_t bFrame = OMF_FIX_F_GRPDEF;
|
---|
1679 | uint16_t idxFrame = pThis->idxGrpFlat;
|
---|
1680 | uint8_t bTarget;
|
---|
1681 | uint16_t idxTarget;
|
---|
1682 | bool fTargetDisp;
|
---|
1683 | uint32_t offTargetDisp;
|
---|
1684 | switch (pOmfSym->enmType)
|
---|
1685 | {
|
---|
1686 | case OMFSYMTYPE_INTERNAL:
|
---|
1687 | case OMFSYMTYPE_PUBDEF:
|
---|
1688 | bTarget = OMF_FIX_T_SEGDEF;
|
---|
1689 | idxTarget = pOmfSym->idxSegDef;
|
---|
1690 | fTargetDisp = true;
|
---|
1691 | offTargetDisp = pElfSym->st_value;
|
---|
1692 | break;
|
---|
1693 |
|
---|
1694 | case OMFSYMTYPE_SEGDEF:
|
---|
1695 | bTarget = OMF_FIX_T_SEGDEF_NO_DISP;
|
---|
1696 | idxTarget = pOmfSym->idxSegDef;
|
---|
1697 | fTargetDisp = false;
|
---|
1698 | offTargetDisp = 0;
|
---|
1699 | break;
|
---|
1700 |
|
---|
1701 | case OMFSYMTYPE_EXTDEF:
|
---|
1702 | bTarget = OMF_FIX_T_EXTDEF_NO_DISP;
|
---|
1703 | idxTarget = pOmfSym->idx;
|
---|
1704 | fTargetDisp = false;
|
---|
1705 | offTargetDisp = 0;
|
---|
1706 | break;
|
---|
1707 |
|
---|
1708 | default:
|
---|
1709 | return error(pThis->pszSrc, "Relocation in segment #%u '%s' references ignored or invalid symbol (%s)\n",
|
---|
1710 | i, pszSegNm, pszSymName);
|
---|
1711 | }
|
---|
1712 |
|
---|
1713 | /* Do COFF relocation type conversion. */
|
---|
1714 | switch (uType)
|
---|
1715 | {
|
---|
1716 | case R_X86_64_64:
|
---|
1717 | {
|
---|
1718 | int64_t iAddend = paRelocs[iReloc].r_addend;
|
---|
1719 | if (iAddend > _1G || iAddend < -_1G)
|
---|
1720 | fRet = error(pThis->pszSrc, "R_X86_64_64 with large addend (%" ELF_FMT_D64 ") at %#x in segment #%u '%s'\n",
|
---|
1721 | iAddend, paRelocs[iReloc].r_offset, i, pszSegNm);
|
---|
1722 | *uLoc.pu64 = iAddend;
|
---|
1723 | fSelfRel = false;
|
---|
1724 | break;
|
---|
1725 | }
|
---|
1726 |
|
---|
1727 | case R_X86_64_32:
|
---|
1728 | case R_X86_64_32S: /* signed, unsigned, whatever. */
|
---|
1729 | fSelfRel = false;
|
---|
1730 | /* fall thru */
|
---|
1731 | case R_X86_64_PC32:
|
---|
1732 | {
|
---|
1733 | /* defaults are ok, just handle the addend. */
|
---|
1734 | int32_t iAddend = paRelocs[iReloc].r_addend;
|
---|
1735 | if (iAddend != paRelocs[iReloc].r_addend)
|
---|
1736 | fRet = error(pThis->pszSrc, "R_X86_64_PC32 with large addend (%d) at %#x in segment #%u '%s'\n",
|
---|
1737 | iAddend, paRelocs[iReloc].r_offset, i, pszSegNm);
|
---|
1738 | if (fSelfRel)
|
---|
1739 | *uLoc.pu32 = iAddend + 4;
|
---|
1740 | else
|
---|
1741 | *uLoc.pu32 = iAddend;
|
---|
1742 | break;
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 | case R_X86_64_NONE:
|
---|
1746 | continue; /* Ignore this one */
|
---|
1747 |
|
---|
1748 | case R_X86_64_GOT32:
|
---|
1749 | case R_X86_64_PLT32:
|
---|
1750 | case R_X86_64_COPY:
|
---|
1751 | case R_X86_64_GLOB_DAT:
|
---|
1752 | case R_X86_64_JMP_SLOT:
|
---|
1753 | case R_X86_64_RELATIVE:
|
---|
1754 | case R_X86_64_GOTPCREL:
|
---|
1755 | case R_X86_64_16:
|
---|
1756 | case R_X86_64_PC16:
|
---|
1757 | case R_X86_64_8:
|
---|
1758 | case R_X86_64_PC8:
|
---|
1759 | case R_X86_64_DTPMOD64:
|
---|
1760 | case R_X86_64_DTPOFF64:
|
---|
1761 | case R_X86_64_TPOFF64:
|
---|
1762 | case R_X86_64_TLSGD:
|
---|
1763 | case R_X86_64_TLSLD:
|
---|
1764 | case R_X86_64_DTPOFF32:
|
---|
1765 | case R_X86_64_GOTTPOFF:
|
---|
1766 | case R_X86_64_TPOFF32:
|
---|
1767 | default:
|
---|
1768 | return error(pThis->pszSrc, "Unsupported fixup type %#x (%s) at rva=%#x in section #%u '%s' against '%s'\n",
|
---|
1769 | uType, g_apszElfAmd64RelTypes[uType], paRelocs[iReloc].r_offset, i, pszSegNm, pszSymName);
|
---|
1770 | }
|
---|
1771 |
|
---|
1772 | /* Add the fixup. */
|
---|
1773 | if (idxFrame == UINT16_MAX)
|
---|
1774 | error(pThis->pszSrc, "idxFrame=UINT16_MAX for %s type=%s\n", pszSymName, g_apszElfAmd64RelTypes[uType]);
|
---|
1775 | fRet = omfWriter_LEDataAddFixup(pThis, offDataRec, fSelfRel, bLocation, bFrame, idxFrame,
|
---|
1776 | bTarget, idxTarget, fTargetDisp, offTargetDisp) && fRet;
|
---|
1777 | }
|
---|
1778 |
|
---|
1779 | /*
|
---|
1780 | * Write the LEDATA and associated FIXUPPs.
|
---|
1781 | */
|
---|
1782 | if (!omfWriter_LEDataEnd(pThis))
|
---|
1783 | return false;
|
---|
1784 |
|
---|
1785 | /*
|
---|
1786 | * Advance.
|
---|
1787 | */
|
---|
1788 | paRelocs += cChunkRelocs;
|
---|
1789 | cRelocs -= cChunkRelocs;
|
---|
1790 | if (cbData > cbChunk)
|
---|
1791 | {
|
---|
1792 | cbData -= cbChunk;
|
---|
1793 | pbData += cbChunk;
|
---|
1794 | }
|
---|
1795 | else
|
---|
1796 | cbData = 0;
|
---|
1797 | off += cbChunk;
|
---|
1798 | cbVirtData -= cbChunk;
|
---|
1799 | }
|
---|
1800 | }
|
---|
1801 |
|
---|
1802 | return fRet;
|
---|
1803 | }
|
---|
1804 |
|
---|
1805 |
|
---|
1806 | static bool convertElfToOmf(const char *pszFile, uint8_t const *pbFile, size_t cbFile, FILE *pDst)
|
---|
1807 | {
|
---|
1808 | /*
|
---|
1809 | * Validate the source file a little.
|
---|
1810 | */
|
---|
1811 | ELFDETAILS ElfStuff;
|
---|
1812 | if (!validateElf(pszFile, pbFile, cbFile, &ElfStuff))
|
---|
1813 | return false;
|
---|
1814 |
|
---|
1815 | /*
|
---|
1816 | * Instantiate the OMF writer.
|
---|
1817 | */
|
---|
1818 | POMFWRITER pThis = omfWriter_Create(pszFile, ElfStuff.pEhdr->e_shnum, ElfStuff.cSymbols, pDst);
|
---|
1819 | if (!pThis)
|
---|
1820 | return false;
|
---|
1821 |
|
---|
1822 | /*
|
---|
1823 | * Write the OMF object file.
|
---|
1824 | */
|
---|
1825 | if (omfWriter_BeginModule(pThis, pszFile))
|
---|
1826 | {
|
---|
1827 | Elf64_Ehdr const *pEhdr = (Elf64_Ehdr const *)pbFile;
|
---|
1828 | Elf64_Shdr const *paShdrs = (Elf64_Shdr const *)&pbFile[pEhdr->e_shoff];
|
---|
1829 | const char *pszStrTab = (const char *)&pbFile[paShdrs[pEhdr->e_shstrndx].sh_offset];
|
---|
1830 |
|
---|
1831 | if ( convertElfSectionsToSegDefsAndGrpDefs(pThis, &ElfStuff)
|
---|
1832 | && convertElfSymbolsToPubDefsAndExtDefs(pThis, &ElfStuff)
|
---|
1833 | && omfWriter_LinkPassSeparator(pThis)
|
---|
1834 | && convertElfSectionsToLeDataAndFixupps(pThis, &ElfStuff, pbFile, cbFile)
|
---|
1835 | && omfWriter_EndModule(pThis) )
|
---|
1836 | {
|
---|
1837 |
|
---|
1838 | omfWriter_Destroy(pThis);
|
---|
1839 | return true;
|
---|
1840 | }
|
---|
1841 | }
|
---|
1842 |
|
---|
1843 | omfWriter_Destroy(pThis);
|
---|
1844 | return false;
|
---|
1845 | }
|
---|
1846 |
|
---|
1847 |
|
---|
1848 |
|
---|
1849 | /*********************************************************************************************************************************
|
---|
1850 | * COFF -> OMF Converter *
|
---|
1851 | *********************************************************************************************************************************/
|
---|
1852 |
|
---|
1853 | /** AMD64 relocation type names for (Microsoft) COFF. */
|
---|
1854 | static const char * const g_apszCoffAmd64RelTypes[] =
|
---|
1855 | {
|
---|
1856 | "ABSOLUTE",
|
---|
1857 | "ADDR64",
|
---|
1858 | "ADDR32",
|
---|
1859 | "ADDR32NB",
|
---|
1860 | "REL32",
|
---|
1861 | "REL32_1",
|
---|
1862 | "REL32_2",
|
---|
1863 | "REL32_3",
|
---|
1864 | "REL32_4",
|
---|
1865 | "REL32_5",
|
---|
1866 | "SECTION",
|
---|
1867 | "SECREL",
|
---|
1868 | "SECREL7",
|
---|
1869 | "TOKEN",
|
---|
1870 | "SREL32",
|
---|
1871 | "PAIR",
|
---|
1872 | "SSPAN32"
|
---|
1873 | };
|
---|
1874 |
|
---|
1875 | /** AMD64 relocation type sizes for (Microsoft) COFF. */
|
---|
1876 | static uint8_t const g_acbCoffAmd64RelTypes[] =
|
---|
1877 | {
|
---|
1878 | 8, /* ABSOLUTE */
|
---|
1879 | 8, /* ADDR64 */
|
---|
1880 | 4, /* ADDR32 */
|
---|
1881 | 4, /* ADDR32NB */
|
---|
1882 | 4, /* REL32 */
|
---|
1883 | 4, /* REL32_1 */
|
---|
1884 | 4, /* REL32_2 */
|
---|
1885 | 4, /* REL32_3 */
|
---|
1886 | 4, /* REL32_4 */
|
---|
1887 | 4, /* REL32_5 */
|
---|
1888 | 2, /* SECTION */
|
---|
1889 | 4, /* SECREL */
|
---|
1890 | 1, /* SECREL7 */
|
---|
1891 | 0, /* TOKEN */
|
---|
1892 | 4, /* SREL32 */
|
---|
1893 | 0, /* PAIR */
|
---|
1894 | 4, /* SSPAN32 */
|
---|
1895 | };
|
---|
1896 |
|
---|
1897 | /** Macro for getting the size of a AMD64 COFF relocation. */
|
---|
1898 | #define COFF_AMD64_RELOC_SIZE(a_Type) ( (a_Type) < RT_ELEMENTS(g_acbCoffAmd64RelTypes) ? g_acbCoffAmd64RelTypes[(a_Type)] : 1)
|
---|
1899 |
|
---|
1900 |
|
---|
1901 | static const char *coffGetSymbolName(PCIMAGE_SYMBOL pSym, const char *pchStrTab, uint32_t cbStrTab, char pszShortName[16])
|
---|
1902 | {
|
---|
1903 | if (pSym->N.Name.Short != 0)
|
---|
1904 | {
|
---|
1905 | memcpy(pszShortName, pSym->N.ShortName, 8);
|
---|
1906 | pszShortName[8] = '\0';
|
---|
1907 | return pszShortName;
|
---|
1908 | }
|
---|
1909 | if (pSym->N.Name.Long < cbStrTab)
|
---|
1910 | {
|
---|
1911 | uint32_t const cbLeft = cbStrTab - pSym->N.Name.Long;
|
---|
1912 | const char *pszRet = pchStrTab + pSym->N.Name.Long;
|
---|
1913 | if (memchr(pszRet, '\0', cbLeft) != NULL)
|
---|
1914 | return pszRet;
|
---|
1915 | }
|
---|
1916 | error("<null>", "Invalid string table index %#x!\n", pSym->N.Name.Long);
|
---|
1917 | return "Invalid Symbol Table Entry";
|
---|
1918 | }
|
---|
1919 |
|
---|
1920 | static bool validateCoff(const char *pszFile, uint8_t const *pbFile, size_t cbFile)
|
---|
1921 | {
|
---|
1922 | /*
|
---|
1923 | * Validate the header and our other expectations.
|
---|
1924 | */
|
---|
1925 | PIMAGE_FILE_HEADER pHdr = (PIMAGE_FILE_HEADER)pbFile;
|
---|
1926 | if (pHdr->Machine != IMAGE_FILE_MACHINE_AMD64)
|
---|
1927 | return error(pszFile, "Expected IMAGE_FILE_MACHINE_AMD64 not %#x\n", pHdr->Machine);
|
---|
1928 | if (pHdr->SizeOfOptionalHeader != 0)
|
---|
1929 | return error(pszFile, "Expected SizeOfOptionalHeader to be zero, not %#x\n", pHdr->SizeOfOptionalHeader);
|
---|
1930 | if (pHdr->NumberOfSections == 0)
|
---|
1931 | return error(pszFile, "Expected NumberOfSections to be non-zero\n");
|
---|
1932 | uint32_t const cbHeaders = pHdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) + sizeof(*pHdr);
|
---|
1933 | if (cbHeaders > cbFile)
|
---|
1934 | return error(pszFile, "Section table goes beyond the end of the of the file (cSections=%#x)\n", pHdr->NumberOfSections);
|
---|
1935 | if (pHdr->NumberOfSymbols)
|
---|
1936 | {
|
---|
1937 | if ( pHdr->PointerToSymbolTable >= cbFile
|
---|
1938 | || pHdr->NumberOfSymbols * (uint64_t)IMAGE_SIZE_OF_SYMBOL > cbFile)
|
---|
1939 | return error(pszFile, "Symbol table goes beyond the end of the of the file (cSyms=%#x, offFile=%#x)\n",
|
---|
1940 | pHdr->NumberOfSymbols, pHdr->PointerToSymbolTable);
|
---|
1941 | }
|
---|
1942 |
|
---|
1943 | return true;
|
---|
1944 | }
|
---|
1945 |
|
---|
1946 |
|
---|
1947 | static bool convertCoffSectionsToSegDefsAndGrpDefs(POMFWRITER pThis, PCIMAGE_SECTION_HEADER paShdrs, uint16_t cSections)
|
---|
1948 | {
|
---|
1949 | /*
|
---|
1950 | * Do the list of names pass.
|
---|
1951 | */
|
---|
1952 | uint16_t idxGrpFlat, idxGrpData;
|
---|
1953 | uint16_t idxClassCode, idxClassData, idxClassDebugSymbols, idxClassDebugTypes;
|
---|
1954 | if ( !omfWriter_LNamesBegin(pThis, true /*fAddZeroEntry*/)
|
---|
1955 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("FLAT"), &idxGrpFlat)
|
---|
1956 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("BS3DATA64_GROUP"), &idxGrpData)
|
---|
1957 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("BS3CLASS64CODE"), &idxClassCode)
|
---|
1958 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("FAR_DATA"), &idxClassData)
|
---|
1959 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("DEBSYM"), &idxClassDebugSymbols)
|
---|
1960 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("DEBTYP"), &idxClassDebugTypes)
|
---|
1961 | )
|
---|
1962 | return false;
|
---|
1963 |
|
---|
1964 | bool fHaveData = false;
|
---|
1965 | for (uint16_t i = 0; i < cSections; i++)
|
---|
1966 | {
|
---|
1967 | /* Copy the name and terminate it. */
|
---|
1968 | char szName[32];
|
---|
1969 | memcpy(szName, paShdrs[i].Name, sizeof(paShdrs[i].Name));
|
---|
1970 | unsigned cchName = sizeof(paShdrs[i].Name);
|
---|
1971 | while (cchName > 0 && RT_C_IS_SPACE(szName[cchName - 1]))
|
---|
1972 | cchName--;
|
---|
1973 | if (cchName == 0)
|
---|
1974 | return error(pThis->pszSrc, "Section #%u has an empty name!\n", i);
|
---|
1975 | szName[cchName] = '\0';
|
---|
1976 |
|
---|
1977 | if ( (paShdrs[i].Characteristics & (IMAGE_SCN_LNK_REMOVE | IMAGE_SCN_LNK_INFO))
|
---|
1978 | || strcmp(szName, ".pdata") == 0 /* Exception stuff, I think, so discard it. */
|
---|
1979 | || strcmp(szName, ".xdata") == 0 /* Ditto. */ )
|
---|
1980 | {
|
---|
1981 | pThis->paSegments[i].iSegDef = UINT16_MAX;
|
---|
1982 | pThis->paSegments[i].iGrpDef = UINT16_MAX;
|
---|
1983 | pThis->paSegments[i].iSegNm = UINT16_MAX;
|
---|
1984 | pThis->paSegments[i].iGrpNm = UINT16_MAX;
|
---|
1985 | pThis->paSegments[i].iClassNm = UINT16_MAX;
|
---|
1986 | pThis->paSegments[i].pszName = NULL;
|
---|
1987 | }
|
---|
1988 | else
|
---|
1989 | {
|
---|
1990 | /* Translate the name, group and class. */
|
---|
1991 | if (strcmp(szName, ".text") == 0)
|
---|
1992 | {
|
---|
1993 | strcpy(szName, "BS3TEXT64");
|
---|
1994 | pThis->paSegments[i].iGrpNm = idxGrpFlat;
|
---|
1995 | pThis->paSegments[i].iClassNm = idxClassCode;
|
---|
1996 | }
|
---|
1997 | else if (strcmp(szName, ".data") == 0)
|
---|
1998 | {
|
---|
1999 | strcpy(szName, "BS3DATA64");
|
---|
2000 | pThis->paSegments[i].iGrpNm = idxGrpData;
|
---|
2001 | pThis->paSegments[i].iClassNm = idxClassData;
|
---|
2002 | }
|
---|
2003 | else if (strcmp(szName, ".bss") == 0)
|
---|
2004 | {
|
---|
2005 | strcpy(szName, "BS3BSS64");
|
---|
2006 | pThis->paSegments[i].iGrpNm = idxGrpData;
|
---|
2007 | pThis->paSegments[i].iClassNm = idxClassData;
|
---|
2008 | }
|
---|
2009 | else if (strcmp(szName, ".rdata") == 0)
|
---|
2010 | {
|
---|
2011 | strcpy(szName, "BS3DATA64CONST");
|
---|
2012 | pThis->paSegments[i].iGrpNm = idxGrpData;
|
---|
2013 | pThis->paSegments[i].iClassNm = idxClassData;
|
---|
2014 | }
|
---|
2015 | else if (strcmp(szName, ".debug$S") == 0)
|
---|
2016 | {
|
---|
2017 | strcpy(szName, "$$SYMBOLS");
|
---|
2018 | pThis->paSegments[i].iGrpNm = UINT16_MAX;
|
---|
2019 | pThis->paSegments[i].iClassNm = idxClassDebugSymbols;
|
---|
2020 | }
|
---|
2021 | else if (strcmp(szName, ".debug$T") == 0)
|
---|
2022 | {
|
---|
2023 | strcpy(szName, "$$TYPES");
|
---|
2024 | pThis->paSegments[i].iGrpNm = UINT16_MAX;
|
---|
2025 | pThis->paSegments[i].iClassNm = idxClassDebugTypes;
|
---|
2026 | }
|
---|
2027 | else if (paShdrs[i].Characteristics & (IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_CNT_CODE))
|
---|
2028 | {
|
---|
2029 | pThis->paSegments[i].iGrpNm = idxGrpFlat;
|
---|
2030 | pThis->paSegments[i].iClassNm = idxClassCode;
|
---|
2031 | error(pThis->pszSrc, "Unknown code segment: '%s'\n", szName);
|
---|
2032 | }
|
---|
2033 | else
|
---|
2034 | {
|
---|
2035 | pThis->paSegments[i].iGrpNm = idxGrpData;
|
---|
2036 | pThis->paSegments[i].iClassNm = idxClassData;
|
---|
2037 | error(pThis->pszSrc, "Unknown data (?) segment: '%s'\n", szName);
|
---|
2038 | }
|
---|
2039 |
|
---|
2040 | /* Save the name. */
|
---|
2041 | pThis->paSegments[i].pszName = strdup(szName);
|
---|
2042 | if (!pThis->paSegments[i].pszName)
|
---|
2043 | return error(pThis->pszSrc, "Out of memory!\n");
|
---|
2044 |
|
---|
2045 | /* Add the section name. */
|
---|
2046 | if (!omfWriter_LNamesAdd(pThis, pThis->paSegments[i].pszName, &pThis->paSegments[i].iSegNm))
|
---|
2047 | return false;
|
---|
2048 |
|
---|
2049 | fHaveData |= pThis->paSegments[i].iGrpNm == idxGrpData;
|
---|
2050 | }
|
---|
2051 | }
|
---|
2052 |
|
---|
2053 | if (!omfWriter_LNamesEnd(pThis))
|
---|
2054 | return false;
|
---|
2055 |
|
---|
2056 | /*
|
---|
2057 | * Emit segment definitions.
|
---|
2058 | */
|
---|
2059 | uint16_t iSegDef = 1; /* Start counting at 1. */
|
---|
2060 | for (uint16_t i = 0; i < cSections; i++)
|
---|
2061 | {
|
---|
2062 | if (pThis->paSegments[i].iSegDef == UINT16_MAX)
|
---|
2063 | continue;
|
---|
2064 |
|
---|
2065 | uint8_t bSegAttr = 0;
|
---|
2066 |
|
---|
2067 | /* The A field. */
|
---|
2068 | switch (paShdrs[i].Characteristics & IMAGE_SCN_ALIGN_MASK)
|
---|
2069 | {
|
---|
2070 | default:
|
---|
2071 | case IMAGE_SCN_ALIGN_1BYTES:
|
---|
2072 | bSegAttr |= 1 << 5;
|
---|
2073 | break;
|
---|
2074 | case IMAGE_SCN_ALIGN_2BYTES:
|
---|
2075 | bSegAttr |= 2 << 5;
|
---|
2076 | break;
|
---|
2077 | case IMAGE_SCN_ALIGN_4BYTES:
|
---|
2078 | bSegAttr |= 5 << 5;
|
---|
2079 | break;
|
---|
2080 | case IMAGE_SCN_ALIGN_8BYTES:
|
---|
2081 | case IMAGE_SCN_ALIGN_16BYTES:
|
---|
2082 | bSegAttr |= 3 << 5;
|
---|
2083 | break;
|
---|
2084 | case IMAGE_SCN_ALIGN_32BYTES:
|
---|
2085 | case IMAGE_SCN_ALIGN_64BYTES:
|
---|
2086 | case IMAGE_SCN_ALIGN_128BYTES:
|
---|
2087 | case IMAGE_SCN_ALIGN_256BYTES:
|
---|
2088 | bSegAttr |= 4 << 5;
|
---|
2089 | break;
|
---|
2090 | case IMAGE_SCN_ALIGN_512BYTES:
|
---|
2091 | case IMAGE_SCN_ALIGN_1024BYTES:
|
---|
2092 | case IMAGE_SCN_ALIGN_2048BYTES:
|
---|
2093 | case IMAGE_SCN_ALIGN_4096BYTES:
|
---|
2094 | case IMAGE_SCN_ALIGN_8192BYTES:
|
---|
2095 | bSegAttr |= 6 << 5; /* page aligned, pharlabs extension. */
|
---|
2096 | break;
|
---|
2097 | }
|
---|
2098 |
|
---|
2099 | /* The C field. */
|
---|
2100 | bSegAttr |= 2 << 2; /* public */
|
---|
2101 |
|
---|
2102 | /* The B field. We don't have 4GB segments, so leave it as zero. */
|
---|
2103 |
|
---|
2104 | /* The D field shall be set as we're doing USE32. */
|
---|
2105 | bSegAttr |= 1;
|
---|
2106 |
|
---|
2107 |
|
---|
2108 | /* Done. */
|
---|
2109 | if (!omfWriter_SegDef(pThis, bSegAttr, paShdrs[i].SizeOfRawData,
|
---|
2110 | pThis->paSegments[i].iSegNm,
|
---|
2111 | pThis->paSegments[i].iClassNm))
|
---|
2112 | return false;
|
---|
2113 | pThis->paSegments[i].iSegDef = iSegDef++;
|
---|
2114 | }
|
---|
2115 |
|
---|
2116 | /*
|
---|
2117 | * Flat group definition (#1) - special, no members.
|
---|
2118 | */
|
---|
2119 | uint16_t iGrpDef = 1;
|
---|
2120 | if ( !omfWriter_GrpDefBegin(pThis, idxGrpFlat)
|
---|
2121 | || !omfWriter_GrpDefEnd(pThis))
|
---|
2122 | return false;
|
---|
2123 | for (uint16_t i = 0; i < cSections; i++)
|
---|
2124 | if (pThis->paSegments[i].iGrpNm == idxGrpFlat)
|
---|
2125 | pThis->paSegments[i].iGrpDef = iGrpDef;
|
---|
2126 | pThis->idxGrpFlat = iGrpDef++;
|
---|
2127 |
|
---|
2128 | /*
|
---|
2129 | * Data group definition (#2).
|
---|
2130 | */
|
---|
2131 | /** @todo do we need to consider missing segments and ordering? */
|
---|
2132 | uint16_t cGrpNms = 0;
|
---|
2133 | uint16_t aiGrpNms[2];
|
---|
2134 | if (fHaveData)
|
---|
2135 | aiGrpNms[cGrpNms++] = idxGrpData;
|
---|
2136 | for (uint32_t iGrpNm = 0; iGrpNm < cGrpNms; iGrpNm++)
|
---|
2137 | {
|
---|
2138 | if (!omfWriter_GrpDefBegin(pThis, aiGrpNms[iGrpNm]))
|
---|
2139 | return false;
|
---|
2140 | for (uint16_t i = 0; i < cSections; i++)
|
---|
2141 | if (pThis->paSegments[i].iGrpNm == aiGrpNms[iGrpNm])
|
---|
2142 | {
|
---|
2143 | pThis->paSegments[i].iGrpDef = iGrpDef;
|
---|
2144 | if (!omfWriter_GrpDefAddSegDef(pThis, pThis->paSegments[i].iSegDef))
|
---|
2145 | return false;
|
---|
2146 | }
|
---|
2147 | if (!omfWriter_GrpDefEnd(pThis))
|
---|
2148 | return false;
|
---|
2149 | iGrpDef++;
|
---|
2150 | }
|
---|
2151 |
|
---|
2152 | return true;
|
---|
2153 | }
|
---|
2154 |
|
---|
2155 | /**
|
---|
2156 | * This is for matching STATIC symbols with value 0 against the section name,
|
---|
2157 | * to see if it's a section reference or symbol at offset 0 reference.
|
---|
2158 | *
|
---|
2159 | * @returns true / false.
|
---|
2160 | * @param pszSymbol The symbol name.
|
---|
2161 | * @param pachSectName8 The section name (8-bytes).
|
---|
2162 | */
|
---|
2163 | static bool isCoffSymbolMatchingSectionName(const char *pszSymbol, uint8_t const pachSectName8[8])
|
---|
2164 | {
|
---|
2165 | uint32_t off = 0;
|
---|
2166 | char ch;
|
---|
2167 | while (off < 8 && (ch = pszSymbol[off]) != '\0')
|
---|
2168 | {
|
---|
2169 | if (ch != pachSectName8[off])
|
---|
2170 | return false;
|
---|
2171 | off++;
|
---|
2172 | }
|
---|
2173 | while (off < 8)
|
---|
2174 | {
|
---|
2175 | if (!RT_C_IS_SPACE((ch = pachSectName8[off])))
|
---|
2176 | return ch == '\0';
|
---|
2177 | off++;
|
---|
2178 | }
|
---|
2179 | return true;
|
---|
2180 | }
|
---|
2181 |
|
---|
2182 | static bool convertCoffSymbolsToPubDefsAndExtDefs(POMFWRITER pThis, PCIMAGE_SYMBOL paSymbols, uint16_t cSymbols,
|
---|
2183 | const char *pchStrTab, PCIMAGE_SECTION_HEADER paShdrs)
|
---|
2184 | {
|
---|
2185 |
|
---|
2186 | if (!cSymbols)
|
---|
2187 | return true;
|
---|
2188 | uint32_t const cbStrTab = *(uint32_t const *)pchStrTab;
|
---|
2189 | char szShort[16];
|
---|
2190 |
|
---|
2191 | /*
|
---|
2192 | * Process the symbols the first.
|
---|
2193 | */
|
---|
2194 | uint32_t iSymImageBase = UINT32_MAX;
|
---|
2195 | uint32_t cAbsSyms = 0;
|
---|
2196 | uint32_t cExtSyms = 0;
|
---|
2197 | uint32_t cPubSyms = 0;
|
---|
2198 | for (uint32_t iSeg = 0; iSeg < pThis->cSegments; iSeg++)
|
---|
2199 | pThis->paSegments[iSeg].cPubDefs = 0;
|
---|
2200 |
|
---|
2201 | for (uint16_t iSym = 0; iSym < cSymbols; iSym++)
|
---|
2202 | {
|
---|
2203 | const char *pszSymName = coffGetSymbolName(&paSymbols[iSym], pchStrTab, cbStrTab, szShort);
|
---|
2204 |
|
---|
2205 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_IGNORED;
|
---|
2206 | pThis->paSymbols[iSym].idx = UINT16_MAX;
|
---|
2207 | pThis->paSymbols[iSym].idxSegDef = UINT16_MAX;
|
---|
2208 | pThis->paSymbols[iSym].idxGrpDef = UINT16_MAX;
|
---|
2209 |
|
---|
2210 | int16_t const idxSection = paSymbols[iSym].SectionNumber;
|
---|
2211 | if ( (idxSection >= 1 && idxSection <= (int32_t)pThis->cSegments)
|
---|
2212 | || idxSection == IMAGE_SYM_ABSOLUTE)
|
---|
2213 | {
|
---|
2214 | switch (paSymbols[iSym].StorageClass)
|
---|
2215 | {
|
---|
2216 | case IMAGE_SYM_CLASS_EXTERNAL:
|
---|
2217 | if (idxSection != IMAGE_SYM_ABSOLUTE)
|
---|
2218 | {
|
---|
2219 | if (pThis->paSegments[idxSection - 1].iSegDef != UINT16_MAX)
|
---|
2220 | {
|
---|
2221 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_PUBDEF;
|
---|
2222 | pThis->paSymbols[iSym].idxSegDef = pThis->paSegments[idxSection - 1].iSegDef;
|
---|
2223 | pThis->paSymbols[iSym].idxGrpDef = pThis->paSegments[idxSection - 1].iGrpDef;
|
---|
2224 | pThis->paSegments[idxSection - 1].cPubDefs++;
|
---|
2225 | cPubSyms++;
|
---|
2226 | }
|
---|
2227 | }
|
---|
2228 | else
|
---|
2229 | {
|
---|
2230 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_PUBDEF;
|
---|
2231 | pThis->paSymbols[iSym].idxSegDef = 0;
|
---|
2232 | pThis->paSymbols[iSym].idxGrpDef = 0;
|
---|
2233 | cAbsSyms++;
|
---|
2234 | }
|
---|
2235 | break;
|
---|
2236 |
|
---|
2237 | case IMAGE_SYM_CLASS_STATIC:
|
---|
2238 | if ( paSymbols[iSym].Value == 0
|
---|
2239 | && idxSection != IMAGE_SYM_ABSOLUTE
|
---|
2240 | && isCoffSymbolMatchingSectionName(pszSymName, paShdrs[idxSection - 1].Name) )
|
---|
2241 | {
|
---|
2242 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_SEGDEF;
|
---|
2243 | pThis->paSymbols[iSym].idxSegDef = pThis->paSegments[idxSection - 1].iSegDef;
|
---|
2244 | pThis->paSymbols[iSym].idxGrpDef = pThis->paSegments[idxSection - 1].iGrpDef;
|
---|
2245 | break;
|
---|
2246 | }
|
---|
2247 | /* fall thru */
|
---|
2248 |
|
---|
2249 | case IMAGE_SYM_CLASS_END_OF_FUNCTION:
|
---|
2250 | case IMAGE_SYM_CLASS_AUTOMATIC:
|
---|
2251 | case IMAGE_SYM_CLASS_REGISTER:
|
---|
2252 | case IMAGE_SYM_CLASS_LABEL:
|
---|
2253 | case IMAGE_SYM_CLASS_MEMBER_OF_STRUCT:
|
---|
2254 | case IMAGE_SYM_CLASS_ARGUMENT:
|
---|
2255 | case IMAGE_SYM_CLASS_STRUCT_TAG:
|
---|
2256 | case IMAGE_SYM_CLASS_MEMBER_OF_UNION:
|
---|
2257 | case IMAGE_SYM_CLASS_UNION_TAG:
|
---|
2258 | case IMAGE_SYM_CLASS_TYPE_DEFINITION:
|
---|
2259 | case IMAGE_SYM_CLASS_ENUM_TAG:
|
---|
2260 | case IMAGE_SYM_CLASS_MEMBER_OF_ENUM:
|
---|
2261 | case IMAGE_SYM_CLASS_REGISTER_PARAM:
|
---|
2262 | case IMAGE_SYM_CLASS_BIT_FIELD:
|
---|
2263 | case IMAGE_SYM_CLASS_BLOCK:
|
---|
2264 | case IMAGE_SYM_CLASS_FUNCTION:
|
---|
2265 | case IMAGE_SYM_CLASS_END_OF_STRUCT:
|
---|
2266 | case IMAGE_SYM_CLASS_FILE:
|
---|
2267 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_INTERNAL;
|
---|
2268 | if (idxSection != IMAGE_SYM_ABSOLUTE)
|
---|
2269 | {
|
---|
2270 | pThis->paSymbols[iSym].idxSegDef = pThis->paSegments[idxSection - 1].iSegDef;
|
---|
2271 | pThis->paSymbols[iSym].idxGrpDef = pThis->paSegments[idxSection - 1].iGrpDef;
|
---|
2272 | }
|
---|
2273 | else
|
---|
2274 | {
|
---|
2275 | pThis->paSymbols[iSym].idxSegDef = 0;
|
---|
2276 | pThis->paSymbols[iSym].idxGrpDef = 0;
|
---|
2277 | }
|
---|
2278 | break;
|
---|
2279 |
|
---|
2280 | case IMAGE_SYM_CLASS_SECTION:
|
---|
2281 | case IMAGE_SYM_CLASS_EXTERNAL_DEF:
|
---|
2282 | case IMAGE_SYM_CLASS_NULL:
|
---|
2283 | case IMAGE_SYM_CLASS_UNDEFINED_LABEL:
|
---|
2284 | case IMAGE_SYM_CLASS_UNDEFINED_STATIC:
|
---|
2285 | case IMAGE_SYM_CLASS_CLR_TOKEN:
|
---|
2286 | case IMAGE_SYM_CLASS_FAR_EXTERNAL:
|
---|
2287 | case IMAGE_SYM_CLASS_WEAK_EXTERNAL:
|
---|
2288 | return error(pThis->pszSrc, "Unsupported storage class value %#x for symbol #%u (%s)\n",
|
---|
2289 | paSymbols[iSym].StorageClass, iSym, pszSymName);
|
---|
2290 |
|
---|
2291 | default:
|
---|
2292 | return error(pThis->pszSrc, "Unknown storage class value %#x for symbol #%u (%s)\n",
|
---|
2293 | paSymbols[iSym].StorageClass, iSym, pszSymName);
|
---|
2294 | }
|
---|
2295 | }
|
---|
2296 | else if (idxSection == IMAGE_SYM_UNDEFINED)
|
---|
2297 | {
|
---|
2298 | if ( paSymbols[iSym].StorageClass == IMAGE_SYM_CLASS_EXTERNAL
|
---|
2299 | || paSymbols[iSym].StorageClass == IMAGE_SYM_CLASS_EXTERNAL_DEF)
|
---|
2300 | {
|
---|
2301 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_EXTDEF;
|
---|
2302 | cExtSyms++;
|
---|
2303 | if (iSymImageBase == UINT32_MAX && strcmp(pszSymName, "__ImageBase") == 0)
|
---|
2304 | iSymImageBase = iSym;
|
---|
2305 | }
|
---|
2306 | else
|
---|
2307 | return error(pThis->pszSrc, "Unknown/unknown storage class value %#x for undefined symbol #%u (%s)\n",
|
---|
2308 | paSymbols[iSym].StorageClass, iSym, pszSymName);
|
---|
2309 | }
|
---|
2310 | else if (idxSection != IMAGE_SYM_DEBUG)
|
---|
2311 | return error(pThis->pszSrc, "Invalid section number %#x for symbol #%u (%s)\n", idxSection, iSym, pszSymName);
|
---|
2312 |
|
---|
2313 | /* Skip AUX symbols. */
|
---|
2314 | uint8_t cAuxSyms = paSymbols[iSym].NumberOfAuxSymbols;
|
---|
2315 | while (cAuxSyms-- > 0)
|
---|
2316 | {
|
---|
2317 | iSym++;
|
---|
2318 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_INVALID;
|
---|
2319 | pThis->paSymbols[iSym].idx = UINT16_MAX;
|
---|
2320 | }
|
---|
2321 | }
|
---|
2322 |
|
---|
2323 | /*
|
---|
2324 | * Emit the PUBDEFs the first time around (see order of records in TIS spec).
|
---|
2325 | */
|
---|
2326 | uint16_t idxPubDef = 1;
|
---|
2327 | if (cPubSyms)
|
---|
2328 | {
|
---|
2329 | for (uint32_t iSeg = 0; iSeg < pThis->cSegments; iSeg++)
|
---|
2330 | if (pThis->paSegments[iSeg].cPubDefs > 0)
|
---|
2331 | {
|
---|
2332 | uint16_t const idxSegDef = pThis->paSegments[iSeg].iSegDef;
|
---|
2333 | if (!omfWriter_PubDefBegin(pThis, pThis->paSegments[iSeg].iGrpDef, idxSegDef))
|
---|
2334 | return false;
|
---|
2335 | for (uint16_t iSym = 0; iSym < cSymbols; iSym++)
|
---|
2336 | if ( pThis->paSymbols[iSym].idxSegDef == idxSegDef
|
---|
2337 | && pThis->paSymbols[iSym].enmType == OMFSYMTYPE_PUBDEF)
|
---|
2338 | {
|
---|
2339 | const char *pszName = coffGetSymbolName(&paSymbols[iSym], pchStrTab, cbStrTab, szShort);
|
---|
2340 | if (!omfWriter_PubDefAdd(pThis, paSymbols[iSym].Value, pszName))
|
---|
2341 | return false;
|
---|
2342 |
|
---|
2343 | /* If the symbol doesn't start with an underscore and is a _c64 or _lm64 symbol,
|
---|
2344 | add an underscore prefixed alias to ease access from 16-bit and 32-bit code. */
|
---|
2345 | size_t cchName = strlen(pszName);
|
---|
2346 | if ( *pszName != '_'
|
---|
2347 | && ( (cchName > 4 && strcmp(&pszName[cchName - 4], "_c64") == 0)
|
---|
2348 | || (cchName > 5 && strcmp(&pszName[cchName - 5], "_lm64") == 0) ) )
|
---|
2349 | {
|
---|
2350 | char szCdeclName[512];
|
---|
2351 | if (cchName > sizeof(szCdeclName) - 2)
|
---|
2352 | cchName = sizeof(szCdeclName) - 2;
|
---|
2353 | szCdeclName[0] = '_';
|
---|
2354 | memcpy(&szCdeclName[1], pszName, cchName);
|
---|
2355 | szCdeclName[cchName + 1] = '\0';
|
---|
2356 | if (!omfWriter_PubDefAdd(pThis, paSymbols[iSym].Value, szCdeclName))
|
---|
2357 | return false;
|
---|
2358 | }
|
---|
2359 |
|
---|
2360 | pThis->paSymbols[iSym].idx = idxPubDef++;
|
---|
2361 | }
|
---|
2362 | if (!omfWriter_PubDefEnd(pThis))
|
---|
2363 | return false;
|
---|
2364 | }
|
---|
2365 | }
|
---|
2366 |
|
---|
2367 | if (cAbsSyms > 0)
|
---|
2368 | {
|
---|
2369 | if (!omfWriter_PubDefBegin(pThis, 0, 0))
|
---|
2370 | return false;
|
---|
2371 | for (uint16_t iSym = 0; iSym < cSymbols; iSym++)
|
---|
2372 | if ( pThis->paSymbols[iSym].idxSegDef == 0
|
---|
2373 | && pThis->paSymbols[iSym].enmType == OMFSYMTYPE_PUBDEF)
|
---|
2374 | {
|
---|
2375 | if (!omfWriter_PubDefAdd(pThis, paSymbols[iSym].Value,
|
---|
2376 | coffGetSymbolName(&paSymbols[iSym], pchStrTab, cbStrTab, szShort)) )
|
---|
2377 | return false;
|
---|
2378 | pThis->paSymbols[iSym].idx = idxPubDef++;
|
---|
2379 | }
|
---|
2380 | if (!omfWriter_PubDefEnd(pThis))
|
---|
2381 | return false;
|
---|
2382 | }
|
---|
2383 |
|
---|
2384 | /*
|
---|
2385 | * Go over the symbol table and emit external definition records.
|
---|
2386 | */
|
---|
2387 | if (!omfWriter_ExtDefBegin(pThis))
|
---|
2388 | return false;
|
---|
2389 | uint16_t idxExtDef = 1;
|
---|
2390 | for (uint16_t iSym = 0; iSym < cSymbols; iSym++)
|
---|
2391 | if (pThis->paSymbols[iSym].enmType == OMFSYMTYPE_EXTDEF)
|
---|
2392 | {
|
---|
2393 | if (!omfWriter_ExtDefAdd(pThis, coffGetSymbolName(&paSymbols[iSym], pchStrTab, cbStrTab, szShort)))
|
---|
2394 | return false;
|
---|
2395 | pThis->paSymbols[iSym].idx = idxExtDef++;
|
---|
2396 | }
|
---|
2397 |
|
---|
2398 | /* Always add an __ImageBase reference, in case we need it to deal with ADDR32NB fixups. */
|
---|
2399 | /** @todo maybe we don't actually need this and could use FLAT instead? */
|
---|
2400 | if (iSymImageBase != UINT32_MAX)
|
---|
2401 | pThis->idxExtImageBase = pThis->paSymbols[iSymImageBase].idx;
|
---|
2402 | else if (omfWriter_ExtDefAdd(pThis, "__ImageBase"))
|
---|
2403 | pThis->idxExtImageBase = idxExtDef;
|
---|
2404 | else
|
---|
2405 | return false;
|
---|
2406 |
|
---|
2407 | if (!omfWriter_ExtDefEnd(pThis))
|
---|
2408 | return false;
|
---|
2409 |
|
---|
2410 | return true;
|
---|
2411 | }
|
---|
2412 |
|
---|
2413 |
|
---|
2414 | static bool convertCoffSectionsToLeDataAndFixupps(POMFWRITER pThis, uint8_t const *pbFile, size_t cbFile,
|
---|
2415 | PCIMAGE_SECTION_HEADER paShdrs, uint16_t cSections,
|
---|
2416 | PCIMAGE_SYMBOL paSymbols, uint16_t cSymbols, const char *pchStrTab)
|
---|
2417 | {
|
---|
2418 | uint32_t const cbStrTab = *(uint32_t const *)pchStrTab;
|
---|
2419 | bool fRet = true;
|
---|
2420 | for (uint32_t i = 0; i < pThis->cSegments; i++)
|
---|
2421 | {
|
---|
2422 | if (pThis->paSegments[i].iSegDef == UINT16_MAX)
|
---|
2423 | continue;
|
---|
2424 |
|
---|
2425 | char szShortName[16];
|
---|
2426 | const char *pszSegNm = pThis->paSegments[i].pszName;
|
---|
2427 | uint16_t cRelocs = paShdrs[i].NumberOfRelocations;
|
---|
2428 | PCIMAGE_RELOCATION paRelocs = (PCIMAGE_RELOCATION)&pbFile[paShdrs[i].PointerToRelocations];
|
---|
2429 | uint32_t cbVirtData = paShdrs[i].SizeOfRawData;
|
---|
2430 | uint32_t cbData = paShdrs[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA ? 0 : cbVirtData;
|
---|
2431 | uint8_t const *pbData = &pbFile[paShdrs[i].PointerToRawData];
|
---|
2432 | uint32_t off = 0;
|
---|
2433 |
|
---|
2434 | /* Check that the relocations are sorted and within the section. */
|
---|
2435 | for (uint32_t iReloc = 1; iReloc < cRelocs; iReloc++)
|
---|
2436 | if (paRelocs[iReloc - 1].u.VirtualAddress >= paRelocs[iReloc].u.VirtualAddress)
|
---|
2437 | return error(pThis->pszSrc, "Section #%u (%s) relocations aren't sorted\n", i, pszSegNm);
|
---|
2438 | if ( cRelocs > 0
|
---|
2439 | && paRelocs[cRelocs - 1].u.VirtualAddress - paShdrs[i].VirtualAddress
|
---|
2440 | + COFF_AMD64_RELOC_SIZE(paRelocs[cRelocs - 1].Type) > cbVirtData)
|
---|
2441 | return error(pThis->pszSrc,
|
---|
2442 | "Section #%u (%s) relocations beyond section data! cbVirtData=%#x RvaFix=%#x RVASeg=%#x type=%#x\n",
|
---|
2443 | i, pszSegNm, cbVirtData, paRelocs[cRelocs - 1].u.VirtualAddress, paShdrs[i].VirtualAddress,
|
---|
2444 | paRelocs[cRelocs - 1].Type);
|
---|
2445 |
|
---|
2446 | /* The OMF record size requires us to split larger sections up. To make
|
---|
2447 | life simple, we fill zeros for unitialized (BSS) stuff. */
|
---|
2448 | const uint32_t cbMaxData = RT_MIN(OMF_MAX_RECORD_PAYLOAD - 1 - (pThis->paSegments[i].iSegDef >= 128) - 4 - 1, _1K);
|
---|
2449 | while (cbVirtData > 0)
|
---|
2450 | {
|
---|
2451 | /* Figure out how many bytes to put out in this chunk. Must make sure
|
---|
2452 | fixups doesn't cross chunk boundraries. ASSUMES sorted relocs. */
|
---|
2453 | uint32_t cChunkRelocs = cRelocs;
|
---|
2454 | uint32_t cbChunk = cbVirtData;
|
---|
2455 | uint32_t uRvaEnd = paShdrs[i].VirtualAddress + off + cbChunk;
|
---|
2456 | if (cbChunk > cbMaxData)
|
---|
2457 | {
|
---|
2458 | cbChunk = cbMaxData;
|
---|
2459 | uRvaEnd = paShdrs[i].VirtualAddress + off + cbChunk;
|
---|
2460 | cChunkRelocs = 0;
|
---|
2461 |
|
---|
2462 | /* Quickly determin the reloc range. */
|
---|
2463 | while ( cChunkRelocs < cRelocs
|
---|
2464 | && paRelocs[cChunkRelocs].u.VirtualAddress < uRvaEnd)
|
---|
2465 | cChunkRelocs++;
|
---|
2466 |
|
---|
2467 | /* Ensure final reloc doesn't go beyond chunk. */
|
---|
2468 | while ( cChunkRelocs > 0
|
---|
2469 | && paRelocs[cChunkRelocs - 1].u.VirtualAddress + COFF_AMD64_RELOC_SIZE(paRelocs[cChunkRelocs - 1].Type)
|
---|
2470 | > uRvaEnd)
|
---|
2471 | {
|
---|
2472 | uint32_t cbDrop = uRvaEnd - paRelocs[cChunkRelocs - 1].u.VirtualAddress;
|
---|
2473 | cbChunk -= cbDrop;
|
---|
2474 | uRvaEnd -= cbDrop;
|
---|
2475 | cChunkRelocs--;
|
---|
2476 | }
|
---|
2477 |
|
---|
2478 | if (!cbVirtData)
|
---|
2479 | return error(pThis->pszSrc, "Wtf? cbVirtData is zero!\n");
|
---|
2480 | }
|
---|
2481 |
|
---|
2482 | /*
|
---|
2483 | * We stash the bytes into the OMF writer record buffer, receiving a
|
---|
2484 | * pointer to the start of it so we can make adjustments if necessary.
|
---|
2485 | */
|
---|
2486 | uint8_t *pbCopy;
|
---|
2487 | if (!omfWriter_LEDataBeginEx(pThis, pThis->paSegments[i].iSegDef, off, cbChunk, cbData, pbData, &pbCopy))
|
---|
2488 | return false;
|
---|
2489 |
|
---|
2490 | /*
|
---|
2491 | * Convert fiuxps.
|
---|
2492 | */
|
---|
2493 | uint32_t const uRvaChunk = paShdrs[i].VirtualAddress + off;
|
---|
2494 | for (uint32_t iReloc = 0; iReloc < cChunkRelocs; iReloc++)
|
---|
2495 | {
|
---|
2496 | /* Get the OMF and COFF data for the symbol the reloc references. */
|
---|
2497 | if (paRelocs[iReloc].SymbolTableIndex >= pThis->cSymbols)
|
---|
2498 | return error(pThis->pszSrc, "Relocation symtab index (%#x) is out of range in segment #%u '%s'\n",
|
---|
2499 | paRelocs[iReloc].SymbolTableIndex, i, pszSegNm);
|
---|
2500 | PCIMAGE_SYMBOL pCoffSym = &paSymbols[paRelocs[iReloc].SymbolTableIndex];
|
---|
2501 | POMFSYMBOL pOmfSym = &pThis->paSymbols[paRelocs[iReloc].SymbolTableIndex];
|
---|
2502 |
|
---|
2503 | /* Calc fixup location in the pending chunk and setup a flexible pointer to it. */
|
---|
2504 | uint16_t offDataRec = (uint16_t)(paRelocs[iReloc].u.VirtualAddress - uRvaChunk);
|
---|
2505 | RTPTRUNION uLoc;
|
---|
2506 | uLoc.pu8 = &pbCopy[offDataRec];
|
---|
2507 |
|
---|
2508 | /* OMF fixup data initialized with typical defaults. */
|
---|
2509 | bool fSelfRel = true;
|
---|
2510 | uint8_t bLocation = OMF_FIX_LOC_32BIT_OFFSET;
|
---|
2511 | uint8_t bFrame = OMF_FIX_F_GRPDEF;
|
---|
2512 | uint16_t idxFrame = pThis->idxGrpFlat;
|
---|
2513 | uint8_t bTarget;
|
---|
2514 | uint16_t idxTarget;
|
---|
2515 | bool fTargetDisp;
|
---|
2516 | uint32_t offTargetDisp;
|
---|
2517 | switch (pOmfSym->enmType)
|
---|
2518 | {
|
---|
2519 | case OMFSYMTYPE_INTERNAL:
|
---|
2520 | case OMFSYMTYPE_PUBDEF:
|
---|
2521 | bTarget = OMF_FIX_T_SEGDEF;
|
---|
2522 | idxTarget = pOmfSym->idxSegDef;
|
---|
2523 | fTargetDisp = true;
|
---|
2524 | offTargetDisp = pCoffSym->Value;
|
---|
2525 | break;
|
---|
2526 |
|
---|
2527 | case OMFSYMTYPE_SEGDEF:
|
---|
2528 | bTarget = OMF_FIX_T_SEGDEF_NO_DISP;
|
---|
2529 | idxTarget = pOmfSym->idxSegDef;
|
---|
2530 | fTargetDisp = false;
|
---|
2531 | offTargetDisp = 0;
|
---|
2532 | break;
|
---|
2533 |
|
---|
2534 | case OMFSYMTYPE_EXTDEF:
|
---|
2535 | bTarget = OMF_FIX_T_EXTDEF_NO_DISP;
|
---|
2536 | idxTarget = pOmfSym->idx;
|
---|
2537 | fTargetDisp = false;
|
---|
2538 | offTargetDisp = 0;
|
---|
2539 | break;
|
---|
2540 |
|
---|
2541 | default:
|
---|
2542 | return error(pThis->pszSrc, "Relocation in segment #%u '%s' references ignored or invalid symbol (%s)\n",
|
---|
2543 | i, pszSegNm, coffGetSymbolName(pCoffSym, pchStrTab, cbStrTab, szShortName));
|
---|
2544 | }
|
---|
2545 |
|
---|
2546 | /* Do COFF relocation type conversion. */
|
---|
2547 | switch (paRelocs[iReloc].Type)
|
---|
2548 | {
|
---|
2549 | case IMAGE_REL_AMD64_ADDR64:
|
---|
2550 | {
|
---|
2551 | uint64_t uAddend = *uLoc.pu64;
|
---|
2552 | if (uAddend > _1G)
|
---|
2553 | fRet = error(pThis->pszSrc, "ADDR64 with large addend (%#llx) at %#x in segment #%u '%s'\n",
|
---|
2554 | uAddend, paRelocs[iReloc].u.VirtualAddress, i, pszSegNm);
|
---|
2555 | fSelfRel = false;
|
---|
2556 | break;
|
---|
2557 | }
|
---|
2558 |
|
---|
2559 | case IMAGE_REL_AMD64_REL32_1:
|
---|
2560 | case IMAGE_REL_AMD64_REL32_2:
|
---|
2561 | case IMAGE_REL_AMD64_REL32_3:
|
---|
2562 | case IMAGE_REL_AMD64_REL32_4:
|
---|
2563 | case IMAGE_REL_AMD64_REL32_5:
|
---|
2564 | /** @todo Check whether OMF read addends from the data or relies on the
|
---|
2565 | * displacement. Also, check what it's relative to. */
|
---|
2566 | *uLoc.pu32 -= paRelocs[iReloc].Type - IMAGE_REL_AMD64_REL32;
|
---|
2567 | break;
|
---|
2568 |
|
---|
2569 | case IMAGE_REL_AMD64_ADDR32:
|
---|
2570 | fSelfRel = false;
|
---|
2571 | break;
|
---|
2572 |
|
---|
2573 | case IMAGE_REL_AMD64_ADDR32NB:
|
---|
2574 | fSelfRel = false;
|
---|
2575 | bFrame = OMF_FIX_F_EXTDEF;
|
---|
2576 | idxFrame = pThis->idxExtImageBase;
|
---|
2577 | break;
|
---|
2578 |
|
---|
2579 | case IMAGE_REL_AMD64_REL32:
|
---|
2580 | /* defaults are ok. */
|
---|
2581 | break;
|
---|
2582 |
|
---|
2583 | case IMAGE_REL_AMD64_SECTION:
|
---|
2584 | bLocation = OMF_FIX_LOC_16BIT_SEGMENT;
|
---|
2585 | /* fall thru */
|
---|
2586 |
|
---|
2587 | case IMAGE_REL_AMD64_SECREL:
|
---|
2588 | fSelfRel = false;
|
---|
2589 | if (pOmfSym->enmType == OMFSYMTYPE_EXTDEF)
|
---|
2590 | {
|
---|
2591 | bFrame = OMF_FIX_F_EXTDEF;
|
---|
2592 | idxFrame = pOmfSym->idx;
|
---|
2593 | }
|
---|
2594 | else
|
---|
2595 | {
|
---|
2596 | bFrame = OMF_FIX_F_SEGDEF;
|
---|
2597 | idxFrame = pOmfSym->idxSegDef;
|
---|
2598 | }
|
---|
2599 | break;
|
---|
2600 |
|
---|
2601 | case IMAGE_REL_AMD64_ABSOLUTE:
|
---|
2602 | continue; /* Ignore it like the PECOFF.DOC says we should. */
|
---|
2603 |
|
---|
2604 | case IMAGE_REL_AMD64_SECREL7:
|
---|
2605 | default:
|
---|
2606 | return error(pThis->pszSrc, "Unsupported fixup type %#x (%s) at rva=%#x in section #%u '%-8.8s'\n",
|
---|
2607 | paRelocs[iReloc].Type,
|
---|
2608 | paRelocs[iReloc].Type < RT_ELEMENTS(g_apszCoffAmd64RelTypes)
|
---|
2609 | ? g_apszCoffAmd64RelTypes[paRelocs[iReloc].Type] : "unknown",
|
---|
2610 | paRelocs[iReloc].u.VirtualAddress, i, paShdrs[i].Name);
|
---|
2611 | }
|
---|
2612 |
|
---|
2613 | /* Add the fixup. */
|
---|
2614 | if (idxFrame == UINT16_MAX)
|
---|
2615 | error(pThis->pszSrc, "idxFrame=UINT16_MAX for %s type=%s\n",
|
---|
2616 | coffGetSymbolName(pCoffSym, pchStrTab, cbStrTab, szShortName),
|
---|
2617 | g_apszCoffAmd64RelTypes[paRelocs[iReloc].Type]);
|
---|
2618 | fRet = omfWriter_LEDataAddFixup(pThis, offDataRec, fSelfRel, bLocation, bFrame, idxFrame,
|
---|
2619 | bTarget, idxTarget, fTargetDisp, offTargetDisp) && fRet;
|
---|
2620 | }
|
---|
2621 |
|
---|
2622 | /*
|
---|
2623 | * Write the LEDATA and associated FIXUPPs.
|
---|
2624 | */
|
---|
2625 | if (!omfWriter_LEDataEnd(pThis))
|
---|
2626 | return false;
|
---|
2627 |
|
---|
2628 | /*
|
---|
2629 | * Advance.
|
---|
2630 | */
|
---|
2631 | paRelocs += cChunkRelocs;
|
---|
2632 | cRelocs -= cChunkRelocs;
|
---|
2633 | if (cbData > cbChunk)
|
---|
2634 | {
|
---|
2635 | cbData -= cbChunk;
|
---|
2636 | pbData += cbChunk;
|
---|
2637 | }
|
---|
2638 | else
|
---|
2639 | cbData = 0;
|
---|
2640 | off += cbChunk;
|
---|
2641 | cbVirtData -= cbChunk;
|
---|
2642 | }
|
---|
2643 | }
|
---|
2644 |
|
---|
2645 | return fRet;
|
---|
2646 | }
|
---|
2647 |
|
---|
2648 |
|
---|
2649 | static bool convertCoffToOmf(const char *pszFile, uint8_t const *pbFile, size_t cbFile, FILE *pDst)
|
---|
2650 | {
|
---|
2651 | /*
|
---|
2652 | * Validate the source file a little.
|
---|
2653 | */
|
---|
2654 | if (!validateCoff(pszFile, pbFile, cbFile))
|
---|
2655 | return false;
|
---|
2656 |
|
---|
2657 | /*
|
---|
2658 | * Instantiate the OMF writer.
|
---|
2659 | */
|
---|
2660 | PIMAGE_FILE_HEADER pHdr = (PIMAGE_FILE_HEADER)pbFile;
|
---|
2661 | POMFWRITER pThis = omfWriter_Create(pszFile, pHdr->NumberOfSections, pHdr->NumberOfSymbols, pDst);
|
---|
2662 | if (!pThis)
|
---|
2663 | return false;
|
---|
2664 |
|
---|
2665 | /*
|
---|
2666 | * Write the OMF object file.
|
---|
2667 | */
|
---|
2668 | if (omfWriter_BeginModule(pThis, pszFile))
|
---|
2669 | {
|
---|
2670 | PCIMAGE_SECTION_HEADER paShdrs = (PCIMAGE_SECTION_HEADER)(pHdr + 1);
|
---|
2671 | PCIMAGE_SYMBOL paSymTab = (PCIMAGE_SYMBOL)&pbFile[pHdr->PointerToSymbolTable];
|
---|
2672 | const char *pchStrTab = (const char *)&paSymTab[pHdr->NumberOfSymbols];
|
---|
2673 | if ( convertCoffSectionsToSegDefsAndGrpDefs(pThis, paShdrs, pHdr->NumberOfSections)
|
---|
2674 | && convertCoffSymbolsToPubDefsAndExtDefs(pThis, paSymTab, pHdr->NumberOfSymbols, pchStrTab, paShdrs)
|
---|
2675 | && omfWriter_LinkPassSeparator(pThis)
|
---|
2676 | && convertCoffSectionsToLeDataAndFixupps(pThis, pbFile, cbFile, paShdrs, pHdr->NumberOfSections,
|
---|
2677 | paSymTab, pHdr->NumberOfSymbols, pchStrTab)
|
---|
2678 | && omfWriter_EndModule(pThis) )
|
---|
2679 | {
|
---|
2680 |
|
---|
2681 | omfWriter_Destroy(pThis);
|
---|
2682 | return true;
|
---|
2683 | }
|
---|
2684 | }
|
---|
2685 |
|
---|
2686 | omfWriter_Destroy(pThis);
|
---|
2687 | return false;
|
---|
2688 | }
|
---|
2689 |
|
---|
2690 |
|
---|
2691 | /*********************************************************************************************************************************
|
---|
2692 | * Mach-O/AMD64 -> OMF/i386 Converter *
|
---|
2693 | *********************************************************************************************************************************/
|
---|
2694 |
|
---|
2695 | //#define MACHO_TO_OMF_CONVERSION
|
---|
2696 | #ifdef MACHO_TO_OMF_CONVERSION
|
---|
2697 |
|
---|
2698 | /** AMD64 relocation type names for Mach-O. */
|
---|
2699 | static const char * const g_apszMachOAmd64RelTypes[] =
|
---|
2700 | {
|
---|
2701 | "X86_64_RELOC_UNSIGNED",
|
---|
2702 | "X86_64_RELOC_SIGNED",
|
---|
2703 | "X86_64_RELOC_BRANCH",
|
---|
2704 | "X86_64_RELOC_GOT_LOAD",
|
---|
2705 | "X86_64_RELOC_GOT",
|
---|
2706 | "X86_64_RELOC_SUBTRACTOR",
|
---|
2707 | "X86_64_RELOC_SIGNED_1",
|
---|
2708 | "X86_64_RELOC_SIGNED_2",
|
---|
2709 | "X86_64_RELOC_SIGNED_4"
|
---|
2710 | };
|
---|
2711 |
|
---|
2712 | /** AMD64 relocation type sizes for Mach-O. */
|
---|
2713 | static uint8_t const g_acbMachOAmd64RelTypes[] =
|
---|
2714 | {
|
---|
2715 | 8, /* X86_64_RELOC_UNSIGNED */
|
---|
2716 | 4, /* X86_64_RELOC_SIGNED */
|
---|
2717 | 4, /* X86_64_RELOC_BRANCH */
|
---|
2718 | 4, /* X86_64_RELOC_GOT_LOAD */
|
---|
2719 | 4, /* X86_64_RELOC_GOT */
|
---|
2720 | 8, /* X86_64_RELOC_SUBTRACTOR */
|
---|
2721 | 4, /* X86_64_RELOC_SIGNED_1 */
|
---|
2722 | 4, /* X86_64_RELOC_SIGNED_2 */
|
---|
2723 | 4, /* X86_64_RELOC_SIGNED_4 */
|
---|
2724 | };
|
---|
2725 |
|
---|
2726 | /** Macro for getting the size of a AMD64 ELF relocation. */
|
---|
2727 | #define ELF_AMD64_RELOC_SIZE(a_Type) ( (a_Type) < RT_ELEMENTS(g_acbElfAmd64RelTypes) ? g_acbElfAmd64RelTypes[(a_Type)] : 1)
|
---|
2728 |
|
---|
2729 |
|
---|
2730 | typedef struct ELFDETAILS
|
---|
2731 | {
|
---|
2732 | /** The ELF header. */
|
---|
2733 | Elf64_Ehdr const *pEhdr;
|
---|
2734 | /** The section header table. */
|
---|
2735 | Elf64_Shdr const *paShdrs;
|
---|
2736 | /** The string table for the section names. */
|
---|
2737 | const char *pchShStrTab;
|
---|
2738 |
|
---|
2739 | /** The symbol table section number. UINT16_MAX if not found. */
|
---|
2740 | uint16_t iSymSh;
|
---|
2741 | /** The string table section number. UINT16_MAX if not found. */
|
---|
2742 | uint16_t iStrSh;
|
---|
2743 |
|
---|
2744 | /** The symbol table. */
|
---|
2745 | Elf64_Sym const *paSymbols;
|
---|
2746 | /** The number of symbols in the symbol table. */
|
---|
2747 | uint32_t cSymbols;
|
---|
2748 |
|
---|
2749 | /** Pointer to the (symbol) string table if found. */
|
---|
2750 | const char *pchStrTab;
|
---|
2751 | /** The string table size. */
|
---|
2752 | size_t cbStrTab;
|
---|
2753 |
|
---|
2754 | } ELFDETAILS;
|
---|
2755 | typedef ELFDETAILS *PELFDETAILS;
|
---|
2756 | typedef ELFDETAILS const *PCELFDETAILS;
|
---|
2757 |
|
---|
2758 |
|
---|
2759 | static bool validateElf(const char *pszFile, uint8_t const *pbFile, size_t cbFile, PELFDETAILS pElfStuff)
|
---|
2760 | {
|
---|
2761 | /*
|
---|
2762 | * Initialize the ELF details structure.
|
---|
2763 | */
|
---|
2764 | memset(pElfStuff, 0, sizeof(*pElfStuff));
|
---|
2765 | pElfStuff->iSymSh = UINT16_MAX;
|
---|
2766 | pElfStuff->iStrSh = UINT16_MAX;
|
---|
2767 |
|
---|
2768 | /*
|
---|
2769 | * Validate the header and our other expectations.
|
---|
2770 | */
|
---|
2771 | Elf64_Ehdr const *pEhdr = (Elf64_Ehdr const *)pbFile;
|
---|
2772 | pElfStuff->pEhdr = pEhdr;
|
---|
2773 | if ( pEhdr->e_ident[EI_CLASS] != ELFCLASS64
|
---|
2774 | || pEhdr->e_ident[EI_DATA] != ELFDATA2LSB
|
---|
2775 | || pEhdr->e_ehsize != sizeof(Elf64_Ehdr)
|
---|
2776 | || pEhdr->e_shentsize != sizeof(Elf64_Shdr)
|
---|
2777 | || pEhdr->e_version != EV_CURRENT )
|
---|
2778 | return error(pszFile, "Unsupported ELF config\n");
|
---|
2779 | if (pEhdr->e_type != ET_REL)
|
---|
2780 | return error(pszFile, "Expected relocatable ELF file (e_type=%d)\n", pEhdr->e_type);
|
---|
2781 | if (pEhdr->e_machine != EM_X86_64)
|
---|
2782 | return error(pszFile, "Expected relocatable ELF file (e_type=%d)\n", pEhdr->e_machine);
|
---|
2783 | if (pEhdr->e_phnum != 0)
|
---|
2784 | return error(pszFile, "Expected e_phnum to be zero not %u\n", pEhdr->e_phnum);
|
---|
2785 | if (pEhdr->e_shnum < 2)
|
---|
2786 | return error(pszFile, "Expected e_shnum to be two or higher\n");
|
---|
2787 | if (pEhdr->e_shstrndx >= pEhdr->e_shnum || pEhdr->e_shstrndx == 0)
|
---|
2788 | return error(pszFile, "Bad e_shstrndx=%u (e_shnum=%u)\n", pEhdr->e_shstrndx, pEhdr->e_shnum);
|
---|
2789 | if ( pEhdr->e_shoff >= cbFile
|
---|
2790 | || pEhdr->e_shoff + pEhdr->e_shnum * sizeof(Elf64_Shdr) > cbFile)
|
---|
2791 | return error(pszFile, "Section table is outside the file (e_shoff=%#llx, e_shnum=%u, cbFile=%#llx)\n",
|
---|
2792 | pEhdr->e_shstrndx, pEhdr->e_shnum, (uint64_t)cbFile);
|
---|
2793 |
|
---|
2794 | /*
|
---|
2795 | * Locate the section name string table.
|
---|
2796 | * We assume it's okay as we only reference it in verbose mode.
|
---|
2797 | */
|
---|
2798 | Elf64_Shdr const *paShdrs = (Elf64_Shdr const *)&pbFile[pEhdr->e_shoff];
|
---|
2799 | pElfStuff->paShdrs = paShdrs;
|
---|
2800 |
|
---|
2801 | Elf64_Xword const cbShStrTab = paShdrs[pEhdr->e_shstrndx].sh_size;
|
---|
2802 | if ( paShdrs[pEhdr->e_shstrndx].sh_offset > cbFile
|
---|
2803 | || cbShStrTab > cbFile
|
---|
2804 | || paShdrs[pEhdr->e_shstrndx].sh_offset + cbShStrTab > cbFile)
|
---|
2805 | return error(pszFile,
|
---|
2806 | "Section string table is outside the file (sh_offset=%#" ELF_FMT_X64 " sh_size=%#" ELF_FMT_X64 " cbFile=%#" ELF_FMT_X64 ")\n",
|
---|
2807 | paShdrs[pEhdr->e_shstrndx].sh_offset, paShdrs[pEhdr->e_shstrndx].sh_size, (Elf64_Xword)cbFile);
|
---|
2808 | const char *pchShStrTab = (const char *)&pbFile[paShdrs[pEhdr->e_shstrndx].sh_offset];
|
---|
2809 | pElfStuff->pchShStrTab = pchShStrTab;
|
---|
2810 |
|
---|
2811 | /*
|
---|
2812 | * Work the section table.
|
---|
2813 | */
|
---|
2814 | bool fRet = true;
|
---|
2815 | for (uint32_t i = 1; i < pEhdr->e_shnum; i++)
|
---|
2816 | {
|
---|
2817 | if (paShdrs[i].sh_name >= cbShStrTab)
|
---|
2818 | return error(pszFile, "Invalid sh_name value (%#x) for section #%u\n", paShdrs[i].sh_name, i);
|
---|
2819 | const char *pszShNm = &pchShStrTab[paShdrs[i].sh_name];
|
---|
2820 |
|
---|
2821 | if ( paShdrs[i].sh_offset > cbFile
|
---|
2822 | || paShdrs[i].sh_size > cbFile
|
---|
2823 | || paShdrs[i].sh_offset + paShdrs[i].sh_size > cbFile)
|
---|
2824 | return error(pszFile, "Section #%u '%s' has data outside the file: %#" ELF_FMT_X64 " LB %#" ELF_FMT_X64 " (cbFile=%#" ELF_FMT_X64 ")\n",
|
---|
2825 | i, pszShNm, paShdrs[i].sh_offset, paShdrs[i].sh_size, (Elf64_Xword)cbFile);
|
---|
2826 | if (g_cVerbose)
|
---|
2827 | printf("shdr[%u]: name=%#x '%s' type=%#x flags=%#" ELF_FMT_X64 " addr=%#" ELF_FMT_X64 " off=%#" ELF_FMT_X64 " size=%#" ELF_FMT_X64 "\n"
|
---|
2828 | " link=%u info=%#x align=%#" ELF_FMT_X64 " entsize=%#" ELF_FMT_X64 "\n",
|
---|
2829 | i, paShdrs[i].sh_name, pszShNm, paShdrs[i].sh_type, paShdrs[i].sh_flags,
|
---|
2830 | paShdrs[i].sh_addr, paShdrs[i].sh_offset, paShdrs[i].sh_size,
|
---|
2831 | paShdrs[i].sh_link, paShdrs[i].sh_info, paShdrs[i].sh_addralign, paShdrs[i].sh_entsize);
|
---|
2832 |
|
---|
2833 | if (paShdrs[i].sh_link >= pEhdr->e_shnum)
|
---|
2834 | return error(pszFile, "Section #%u '%s' links to a section outside the section table: %#x, max %#x\n",
|
---|
2835 | i, pszShNm, paShdrs[i].sh_link, pEhdr->e_shnum);
|
---|
2836 | if (!RT_IS_POWER_OF_TWO(paShdrs[i].sh_addralign))
|
---|
2837 | return error(pszFile, "Section #%u '%s' alignment value is not a power of two: %#" ELF_FMT_X64 "\n",
|
---|
2838 | i, pszShNm, paShdrs[i].sh_addralign);
|
---|
2839 | if (!RT_IS_POWER_OF_TWO(paShdrs[i].sh_addralign))
|
---|
2840 | return error(pszFile, "Section #%u '%s' alignment value is not a power of two: %#" ELF_FMT_X64 "\n",
|
---|
2841 | i, pszShNm, paShdrs[i].sh_addralign);
|
---|
2842 | if (paShdrs[i].sh_addr != 0)
|
---|
2843 | return error(pszFile, "Section #%u '%s' has non-zero address: %#" ELF_FMT_X64 "\n", i, pszShNm, paShdrs[i].sh_addr);
|
---|
2844 |
|
---|
2845 | if (paShdrs[i].sh_type == SHT_RELA)
|
---|
2846 | {
|
---|
2847 | if (paShdrs[i].sh_entsize != sizeof(Elf64_Rela))
|
---|
2848 | return error(pszFile, "Expected sh_entsize to be %u not %u for section #%u (%s)\n", (unsigned)sizeof(Elf64_Rela),
|
---|
2849 | paShdrs[i].sh_entsize, i, pszShNm);
|
---|
2850 | uint32_t const cRelocs = paShdrs[i].sh_size / sizeof(Elf64_Rela);
|
---|
2851 | if (cRelocs * sizeof(Elf64_Rela) != paShdrs[i].sh_size)
|
---|
2852 | return error(pszFile, "Uneven relocation entry count in #%u (%s): sh_size=%#" ELF_FMT_X64 "\n",
|
---|
2853 | i, pszShNm, paShdrs[i].sh_size);
|
---|
2854 | if ( paShdrs[i].sh_offset > cbFile
|
---|
2855 | || paShdrs[i].sh_size >= cbFile
|
---|
2856 | || paShdrs[i].sh_offset + paShdrs[i].sh_size > cbFile)
|
---|
2857 | return error(pszFile, "The content of section #%u '%s' is outside the file (%#" ELF_FMT_X64 " LB %#" ELF_FMT_X64 ", cbFile=%#lx)\n",
|
---|
2858 | i, pszShNm, paShdrs[i].sh_offset, paShdrs[i].sh_size, (unsigned long)cbFile);
|
---|
2859 | if (paShdrs[i].sh_info != i - 1)
|
---|
2860 | return error(pszFile, "Expected relocation section #%u (%s) to link to previous section: sh_info=%#u\n",
|
---|
2861 | i, pszShNm, (unsigned)paShdrs[i].sh_link);
|
---|
2862 | if (paShdrs[paShdrs[i].sh_link].sh_type != SHT_SYMTAB)
|
---|
2863 | return error(pszFile, "Expected relocation section #%u (%s) to link to symbol table: sh_link=%#u -> sh_type=%#x\n",
|
---|
2864 | i, pszShNm, (unsigned)paShdrs[i].sh_link, (unsigned)paShdrs[paShdrs[i].sh_link].sh_type);
|
---|
2865 | uint32_t cSymbols = paShdrs[paShdrs[i].sh_link].sh_size / paShdrs[paShdrs[i].sh_link].sh_entsize;
|
---|
2866 |
|
---|
2867 | Elf64_Rela const *paRelocs = (Elf64_Rela *)&pbFile[paShdrs[i].sh_offset];
|
---|
2868 | for (uint32_t j = 0; j < cRelocs; j++)
|
---|
2869 | {
|
---|
2870 | uint8_t const bType = ELF64_R_TYPE(paRelocs[j].r_info);
|
---|
2871 | if (RT_UNLIKELY(bType >= R_X86_64_COUNT))
|
---|
2872 | fRet = error(pszFile,
|
---|
2873 | "%#018" ELF_FMT_X64 " %#018" ELF_FMT_X64 ": unknown fix up %#x (%+" ELF_FMT_D64 ")\n",
|
---|
2874 | paRelocs[j].r_offset, paRelocs[j].r_info, bType, paRelocs[j].r_addend);
|
---|
2875 | if (RT_UNLIKELY( j > 1
|
---|
2876 | && paRelocs[j].r_offset <= paRelocs[j - 1].r_offset
|
---|
2877 | && paRelocs[j].r_offset + ELF_AMD64_RELOC_SIZE(ELF64_R_TYPE(paRelocs[j].r_info))
|
---|
2878 | < paRelocs[j - 1].r_offset ))
|
---|
2879 | fRet = error(pszFile,
|
---|
2880 | "%#018" ELF_FMT_X64 " %#018" ELF_FMT_X64 ": out of offset order (prev %" ELF_FMT_X64 ")\n",
|
---|
2881 | paRelocs[j].r_offset, paRelocs[j].r_info, paRelocs[j - 1].r_offset);
|
---|
2882 | uint32_t const iSymbol = ELF64_R_SYM(paRelocs[j].r_info);
|
---|
2883 | if (RT_UNLIKELY(iSymbol >= cSymbols))
|
---|
2884 | fRet = error(pszFile,
|
---|
2885 | "%#018" ELF_FMT_X64 " %#018" ELF_FMT_X64 ": symbol index (%#x) out of bounds (%#x)\n",
|
---|
2886 | paRelocs[j].r_offset, paRelocs[j].r_info, iSymbol, cSymbols);
|
---|
2887 | }
|
---|
2888 | if (RT_UNLIKELY( cRelocs > 0
|
---|
2889 | && fRet
|
---|
2890 | && ( paRelocs[cRelocs - 1].r_offset > paShdrs[i - 1].sh_size
|
---|
2891 | || paRelocs[cRelocs - 1].r_offset + ELF_AMD64_RELOC_SIZE(ELF64_R_TYPE(paRelocs[cRelocs-1].r_info))
|
---|
2892 | > paShdrs[i - 1].sh_size )))
|
---|
2893 | fRet = error(pszFile,
|
---|
2894 | "%#018" ELF_FMT_X64 " %#018" ELF_FMT_X64 ": out of bounds (sh_size %" ELF_FMT_X64 ")\n",
|
---|
2895 | paRelocs[cRelocs - 1].r_offset, paRelocs[cRelocs - 1].r_info, paShdrs[i - 1].sh_size);
|
---|
2896 |
|
---|
2897 | }
|
---|
2898 | else if (paShdrs[i].sh_type == SHT_REL)
|
---|
2899 | fRet = error(pszFile, "Section #%u '%s': Unexpected SHT_REL section\n", i, pszShNm);
|
---|
2900 | else if (paShdrs[i].sh_type == SHT_SYMTAB)
|
---|
2901 | {
|
---|
2902 | if (paShdrs[i].sh_entsize != sizeof(Elf64_Sym))
|
---|
2903 | fRet = error(pszFile, "Section #%u '%s': Unsupported symbol table entry size in : #%u (expected #%u)\n",
|
---|
2904 | i, pszShNm, paShdrs[i].sh_entsize, sizeof(Elf64_Sym));
|
---|
2905 | Elf64_Xword const cSymbols = paShdrs[i].sh_size / paShdrs[i].sh_entsize;
|
---|
2906 | if (cSymbols * paShdrs[i].sh_entsize != paShdrs[i].sh_size)
|
---|
2907 | fRet = error(pszFile, "Section #%u '%s': Size not a multiple of entry size: %#" ELF_FMT_X64 " %% %#" ELF_FMT_X64 " = %#" ELF_FMT_X64 "\n",
|
---|
2908 | i, pszShNm, paShdrs[i].sh_size, paShdrs[i].sh_entsize, paShdrs[i].sh_size % paShdrs[i].sh_entsize);
|
---|
2909 | if (cSymbols > UINT32_MAX)
|
---|
2910 | fRet = error(pszFile, "Section #%u '%s': too many symbols: %" ELF_FMT_X64 "\n",
|
---|
2911 | i, pszShNm, paShdrs[i].sh_size, cSymbols);
|
---|
2912 |
|
---|
2913 | if (pElfStuff->iSymSh == UINT16_MAX)
|
---|
2914 | {
|
---|
2915 | pElfStuff->iSymSh = (uint16_t)i;
|
---|
2916 | pElfStuff->paSymbols = (Elf64_Sym const *)&pbFile[paShdrs[i].sh_offset];
|
---|
2917 | pElfStuff->cSymbols = cSymbols;
|
---|
2918 |
|
---|
2919 | if (paShdrs[i].sh_link != 0)
|
---|
2920 | {
|
---|
2921 | /* Note! The symbol string table section header may not have been validated yet! */
|
---|
2922 | Elf64_Shdr const *pStrTabShdr = &paShdrs[paShdrs[i].sh_link];
|
---|
2923 | pElfStuff->iStrSh = paShdrs[i].sh_link;
|
---|
2924 | pElfStuff->pchStrTab = (const char *)&pbFile[pStrTabShdr->sh_offset];
|
---|
2925 | pElfStuff->cbStrTab = (size_t)pStrTabShdr->sh_size;
|
---|
2926 | }
|
---|
2927 | else
|
---|
2928 | fRet = error(pszFile, "Section #%u '%s': String table link is out of bounds (%#x)\n",
|
---|
2929 | i, pszShNm, paShdrs[i].sh_link);
|
---|
2930 | }
|
---|
2931 | else
|
---|
2932 | fRet = error(pszFile, "Section #%u '%s': Found additonal symbol table, previous in #%u\n",
|
---|
2933 | i, pszShNm, pElfStuff->iSymSh);
|
---|
2934 | }
|
---|
2935 | }
|
---|
2936 | return fRet;
|
---|
2937 | }
|
---|
2938 |
|
---|
2939 | static bool convertElfSectionsToSegDefsAndGrpDefs(POMFWRITER pThis, PCELFDETAILS pElfStuff)
|
---|
2940 | {
|
---|
2941 | /*
|
---|
2942 | * Do the list of names pass.
|
---|
2943 | */
|
---|
2944 | uint16_t idxGrpFlat, idxGrpData;
|
---|
2945 | uint16_t idxClassCode, idxClassData, idxClassDwarf;
|
---|
2946 | if ( !omfWriter_LNamesBegin(pThis, true /*fAddZeroEntry*/)
|
---|
2947 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("FLAT"), &idxGrpFlat)
|
---|
2948 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("BS3DATA64_GROUP"), &idxGrpData)
|
---|
2949 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("BS3CLASS64CODE"), &idxClassCode)
|
---|
2950 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("FAR_DATA"), &idxClassData)
|
---|
2951 | || !omfWriter_LNamesAddN(pThis, RT_STR_TUPLE("DWARF"), &idxClassDwarf)
|
---|
2952 | )
|
---|
2953 | return false;
|
---|
2954 |
|
---|
2955 | bool fHaveData = false;
|
---|
2956 | Elf64_Shdr const *pShdr = &pElfStuff->paShdrs[1];
|
---|
2957 | Elf64_Half const cSections = pElfStuff->pEhdr->e_shnum;
|
---|
2958 | for (Elf64_Half i = 1; i < cSections; i++, pShdr++)
|
---|
2959 | {
|
---|
2960 | const char *pszName = &pElfStuff->pchShStrTab[pShdr->sh_name];
|
---|
2961 | if (*pszName == '\0')
|
---|
2962 | return error(pThis->pszSrc, "Section #%u has an empty name!\n", i);
|
---|
2963 |
|
---|
2964 | switch (pShdr->sh_type)
|
---|
2965 | {
|
---|
2966 | case SHT_PROGBITS:
|
---|
2967 | case SHT_NOBITS:
|
---|
2968 | /* We drop a few sections we don't want:. */
|
---|
2969 | if ( strcmp(pszName, ".comment") != 0 /* compiler info */
|
---|
2970 | && strcmp(pszName, ".note.GNU-stack") != 0 /* some empty section for hinting the linker/whatever */
|
---|
2971 | && strcmp(pszName, ".eh_frame") != 0 /* unwind / exception info */
|
---|
2972 | )
|
---|
2973 | {
|
---|
2974 | pThis->paSegments[i].iSegDef = UINT16_MAX;
|
---|
2975 | pThis->paSegments[i].iGrpDef = UINT16_MAX;
|
---|
2976 |
|
---|
2977 | /* Translate the name and determine group and class.
|
---|
2978 | Note! We currently strip sub-sections. */
|
---|
2979 | if ( strcmp(pszName, ".text") == 0
|
---|
2980 | || strncmp(pszName, RT_STR_TUPLE(".text.")) == 0)
|
---|
2981 | {
|
---|
2982 | pszName = "BS3TEXT64";
|
---|
2983 | pThis->paSegments[i].iGrpNm = idxGrpFlat;
|
---|
2984 | pThis->paSegments[i].iClassNm = idxClassCode;
|
---|
2985 | }
|
---|
2986 | else if ( strcmp(pszName, ".data") == 0
|
---|
2987 | || strncmp(pszName, RT_STR_TUPLE(".data.")) == 0)
|
---|
2988 | {
|
---|
2989 | pszName = "BS3DATA64";
|
---|
2990 | pThis->paSegments[i].iGrpNm = idxGrpData;
|
---|
2991 | pThis->paSegments[i].iClassNm = idxClassData;
|
---|
2992 | }
|
---|
2993 | else if (strcmp(pszName, ".bss") == 0)
|
---|
2994 | {
|
---|
2995 | pszName = "BS3BSS64";
|
---|
2996 | pThis->paSegments[i].iGrpNm = idxGrpData;
|
---|
2997 | pThis->paSegments[i].iClassNm = idxClassData;
|
---|
2998 | }
|
---|
2999 | else if ( strcmp(pszName, ".rodata") == 0
|
---|
3000 | || strncmp(pszName, RT_STR_TUPLE(".rodata.")) == 0)
|
---|
3001 | {
|
---|
3002 | pszName = "BS3DATA64CONST";
|
---|
3003 | pThis->paSegments[i].iGrpNm = idxGrpData;
|
---|
3004 | pThis->paSegments[i].iClassNm = idxClassData;
|
---|
3005 | }
|
---|
3006 | else if (strncmp(pszName, RT_STR_TUPLE(".debug_")) == 0)
|
---|
3007 | {
|
---|
3008 | pThis->paSegments[i].iGrpNm = UINT16_MAX;
|
---|
3009 | pThis->paSegments[i].iClassNm = idxClassDwarf;
|
---|
3010 | }
|
---|
3011 | else
|
---|
3012 | {
|
---|
3013 | pThis->paSegments[i].iGrpNm = idxGrpData;
|
---|
3014 | pThis->paSegments[i].iClassNm = idxClassData;
|
---|
3015 | error(pThis->pszSrc, "Unknown data (?) segment: '%s'\n", pszName);
|
---|
3016 | }
|
---|
3017 |
|
---|
3018 | /* Save the name. */
|
---|
3019 | pThis->paSegments[i].pszName = strdup(pszName);
|
---|
3020 | if (!pThis->paSegments[i].pszName)
|
---|
3021 | return error(pThis->pszSrc, "Out of memory!\n");
|
---|
3022 |
|
---|
3023 | /* Add the section name. */
|
---|
3024 | if (!omfWriter_LNamesAdd(pThis, pThis->paSegments[i].pszName, &pThis->paSegments[i].iSegNm))
|
---|
3025 | return false;
|
---|
3026 |
|
---|
3027 | fHaveData |= pThis->paSegments[i].iGrpDef == idxGrpData;
|
---|
3028 | break;
|
---|
3029 | }
|
---|
3030 | /* fall thru */
|
---|
3031 |
|
---|
3032 | default:
|
---|
3033 | pThis->paSegments[i].iSegDef = UINT16_MAX;
|
---|
3034 | pThis->paSegments[i].iGrpDef = UINT16_MAX;
|
---|
3035 | pThis->paSegments[i].iSegNm = UINT16_MAX;
|
---|
3036 | pThis->paSegments[i].iGrpNm = UINT16_MAX;
|
---|
3037 | pThis->paSegments[i].iClassNm = UINT16_MAX;
|
---|
3038 | pThis->paSegments[i].pszName = NULL;
|
---|
3039 | break;
|
---|
3040 | }
|
---|
3041 | }
|
---|
3042 |
|
---|
3043 | if (!omfWriter_LNamesEnd(pThis))
|
---|
3044 | return false;
|
---|
3045 |
|
---|
3046 | /*
|
---|
3047 | * Emit segment definitions.
|
---|
3048 | */
|
---|
3049 | uint16_t iSegDef = 1; /* Start counting at 1. */
|
---|
3050 | pShdr = &pElfStuff->paShdrs[1];
|
---|
3051 | for (Elf64_Half i = 1; i < cSections; i++, pShdr++)
|
---|
3052 | {
|
---|
3053 | if (pThis->paSegments[i].iSegNm == UINT16_MAX)
|
---|
3054 | continue;
|
---|
3055 |
|
---|
3056 | uint8_t bSegAttr = 0;
|
---|
3057 |
|
---|
3058 | /* The A field. */
|
---|
3059 | switch (pShdr->sh_addralign)
|
---|
3060 | {
|
---|
3061 | case 0:
|
---|
3062 | case 1:
|
---|
3063 | bSegAttr |= 1 << 5;
|
---|
3064 | break;
|
---|
3065 | case 2:
|
---|
3066 | bSegAttr |= 2 << 5;
|
---|
3067 | break;
|
---|
3068 | case 4:
|
---|
3069 | bSegAttr |= 5 << 5;
|
---|
3070 | break;
|
---|
3071 | case 8:
|
---|
3072 | case 16:
|
---|
3073 | bSegAttr |= 3 << 5;
|
---|
3074 | break;
|
---|
3075 | case 32:
|
---|
3076 | case 64:
|
---|
3077 | case 128:
|
---|
3078 | case 256:
|
---|
3079 | bSegAttr |= 4 << 5;
|
---|
3080 | break;
|
---|
3081 | default:
|
---|
3082 | bSegAttr |= 6 << 5; /* page aligned, pharlabs extension. */
|
---|
3083 | break;
|
---|
3084 | }
|
---|
3085 |
|
---|
3086 | /* The C field. */
|
---|
3087 | bSegAttr |= 2 << 2; /* public */
|
---|
3088 |
|
---|
3089 | /* The B field. We don't have 4GB segments, so leave it as zero. */
|
---|
3090 |
|
---|
3091 | /* The D field shall be set as we're doing USE32. */
|
---|
3092 | bSegAttr |= 1;
|
---|
3093 |
|
---|
3094 |
|
---|
3095 | /* Done. */
|
---|
3096 | if (!omfWriter_SegDef(pThis, bSegAttr, (uint32_t)pShdr->sh_size,
|
---|
3097 | pThis->paSegments[i].iSegNm,
|
---|
3098 | pThis->paSegments[i].iClassNm))
|
---|
3099 | return false;
|
---|
3100 | pThis->paSegments[i].iSegDef = iSegDef++;
|
---|
3101 | }
|
---|
3102 |
|
---|
3103 | /*
|
---|
3104 | * Flat group definition (#1) - special, no members.
|
---|
3105 | */
|
---|
3106 | uint16_t iGrpDef = 1;
|
---|
3107 | if ( !omfWriter_GrpDefBegin(pThis, idxGrpFlat)
|
---|
3108 | || !omfWriter_GrpDefEnd(pThis))
|
---|
3109 | return false;
|
---|
3110 | for (uint16_t i = 0; i < cSections; i++)
|
---|
3111 | if (pThis->paSegments[i].iGrpNm == idxGrpFlat)
|
---|
3112 | pThis->paSegments[i].iGrpDef = iGrpDef;
|
---|
3113 | pThis->idxGrpFlat = iGrpDef++;
|
---|
3114 |
|
---|
3115 | /*
|
---|
3116 | * Data group definition (#2).
|
---|
3117 | */
|
---|
3118 | /** @todo do we need to consider missing segments and ordering? */
|
---|
3119 | uint16_t cGrpNms = 0;
|
---|
3120 | uint16_t aiGrpNms[2];
|
---|
3121 | if (fHaveData)
|
---|
3122 | aiGrpNms[cGrpNms++] = idxGrpData;
|
---|
3123 | for (uint32_t iGrpNm = 0; iGrpNm < cGrpNms; iGrpNm++)
|
---|
3124 | {
|
---|
3125 | if (!omfWriter_GrpDefBegin(pThis, aiGrpNms[iGrpNm]))
|
---|
3126 | return false;
|
---|
3127 | for (uint16_t i = 0; i < cSections; i++)
|
---|
3128 | if (pThis->paSegments[i].iGrpNm == aiGrpNms[iGrpNm])
|
---|
3129 | {
|
---|
3130 | pThis->paSegments[i].iGrpDef = iGrpDef;
|
---|
3131 | if (!omfWriter_GrpDefAddSegDef(pThis, pThis->paSegments[i].iSegDef))
|
---|
3132 | return false;
|
---|
3133 | }
|
---|
3134 | if (!omfWriter_GrpDefEnd(pThis))
|
---|
3135 | return false;
|
---|
3136 | iGrpDef++;
|
---|
3137 | }
|
---|
3138 |
|
---|
3139 | return true;
|
---|
3140 | }
|
---|
3141 |
|
---|
3142 | static bool convertElfSymbolsToPubDefsAndExtDefs(POMFWRITER pThis, PCELFDETAILS pElfStuff)
|
---|
3143 | {
|
---|
3144 | if (!pElfStuff->cSymbols)
|
---|
3145 | return true;
|
---|
3146 |
|
---|
3147 | /*
|
---|
3148 | * Process the symbols the first.
|
---|
3149 | */
|
---|
3150 | uint32_t cAbsSyms = 0;
|
---|
3151 | uint32_t cExtSyms = 0;
|
---|
3152 | uint32_t cPubSyms = 0;
|
---|
3153 | for (uint32_t iSeg = 0; iSeg < pThis->cSegments; iSeg++)
|
---|
3154 | pThis->paSegments[iSeg].cPubDefs = 0;
|
---|
3155 |
|
---|
3156 | uint32_t const cSections = pElfStuff->pEhdr->e_shnum;
|
---|
3157 | uint32_t const cSymbols = pElfStuff->cSymbols;
|
---|
3158 | Elf64_Sym const * const paSymbols = pElfStuff->paSymbols;
|
---|
3159 | for (uint32_t iSym = 0; iSym < cSymbols; iSym++)
|
---|
3160 | {
|
---|
3161 | const uint8_t bBind = ELF64_ST_BIND(paSymbols[iSym].st_info);
|
---|
3162 | const uint8_t bType = ELF64_ST_TYPE(paSymbols[iSym].st_info);
|
---|
3163 | const char *pszSymName = &pElfStuff->pchStrTab[paSymbols[iSym].st_name];
|
---|
3164 | if ( *pszSymName == '\0'
|
---|
3165 | && bType == STT_SECTION
|
---|
3166 | && paSymbols[iSym].st_shndx < cSections)
|
---|
3167 | pszSymName = &pElfStuff->pchShStrTab[pElfStuff->paShdrs[paSymbols[iSym].st_shndx].sh_name];
|
---|
3168 |
|
---|
3169 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_IGNORED;
|
---|
3170 | pThis->paSymbols[iSym].idx = UINT16_MAX;
|
---|
3171 | pThis->paSymbols[iSym].idxSegDef = UINT16_MAX;
|
---|
3172 | pThis->paSymbols[iSym].idxGrpDef = UINT16_MAX;
|
---|
3173 |
|
---|
3174 | uint32_t const idxSection = paSymbols[iSym].st_shndx;
|
---|
3175 | if (idxSection == SHN_UNDEF)
|
---|
3176 | {
|
---|
3177 | if (bBind == STB_GLOBAL)
|
---|
3178 | {
|
---|
3179 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_EXTDEF;
|
---|
3180 | cExtSyms++;
|
---|
3181 | if (*pszSymName == '\0')
|
---|
3182 | return error(pThis->pszSrc, "External symbol #%u (%s) has an empty name.\n", iSym, pszSymName);
|
---|
3183 | }
|
---|
3184 | else if (bBind != STB_LOCAL || iSym != 0) /* Entry zero is usually a dummy. */
|
---|
3185 | return error(pThis->pszSrc, "Unsupported or invalid bind type %#x for undefined symbol #%u (%s)\n",
|
---|
3186 | bBind, iSym, pszSymName);
|
---|
3187 | }
|
---|
3188 | else if (idxSection < cSections)
|
---|
3189 | {
|
---|
3190 | pThis->paSymbols[iSym].idxSegDef = pThis->paSegments[idxSection].iSegDef;
|
---|
3191 | pThis->paSymbols[iSym].idxGrpDef = pThis->paSegments[idxSection].iGrpDef;
|
---|
3192 | if (bBind == STB_GLOBAL)
|
---|
3193 | {
|
---|
3194 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_PUBDEF;
|
---|
3195 | pThis->paSegments[idxSection].cPubDefs++;
|
---|
3196 | cPubSyms++;
|
---|
3197 | if (bType == STT_SECTION)
|
---|
3198 | return error(pThis->pszSrc, "Don't know how to export STT_SECTION symbol #%u (%s)\n", iSym, pszSymName);
|
---|
3199 | if (*pszSymName == '\0')
|
---|
3200 | return error(pThis->pszSrc, "Public symbol #%u (%s) has an empty name.\n", iSym, pszSymName);
|
---|
3201 | }
|
---|
3202 | else if (bType == STT_SECTION)
|
---|
3203 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_SEGDEF;
|
---|
3204 | else
|
---|
3205 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_INTERNAL;
|
---|
3206 | }
|
---|
3207 | else if (idxSection == SHN_ABS)
|
---|
3208 | {
|
---|
3209 | if (bType != STT_FILE)
|
---|
3210 | {
|
---|
3211 | if (bBind == STB_GLOBAL)
|
---|
3212 | {
|
---|
3213 | pThis->paSymbols[iSym].enmType = OMFSYMTYPE_PUBDEF;
|
---|
3214 | pThis->paSymbols[iSym].idxSegDef = 0;
|
---|
3215 | pThis->paSymbols[iSym].idxGrpDef = 0;
|
---|
3216 | cAbsSyms++;
|
---|
3217 | if (*pszSymName == '\0')
|
---|
3218 | return error(pThis->pszSrc, "Public absolute symbol #%u (%s) has an empty name.\n", iSym, pszSymName);
|
---|
3219 | }
|
---|
3220 | else
|
---|
3221 | return error(pThis->pszSrc, "Unsupported or invalid bind type %#x for absolute symbol #%u (%s)\n",
|
---|
3222 | bBind, iSym, pszSymName);
|
---|
3223 | }
|
---|
3224 | }
|
---|
3225 | else
|
---|
3226 | return error(pThis->pszSrc, "Unsupported or invalid section number %#x for symbol #%u (%s)\n",
|
---|
3227 | idxSection, iSym, pszSymName);
|
---|
3228 | }
|
---|
3229 |
|
---|
3230 | /*
|
---|
3231 | * Emit the PUBDEFs the first time around (see order of records in TIS spec).
|
---|
3232 | */
|
---|
3233 | uint16_t idxPubDef = 1;
|
---|
3234 | if (cPubSyms)
|
---|
3235 | {
|
---|
3236 | for (uint32_t iSeg = 0; iSeg < pThis->cSegments; iSeg++)
|
---|
3237 | if (pThis->paSegments[iSeg].cPubDefs > 0)
|
---|
3238 | {
|
---|
3239 | uint16_t const idxSegDef = pThis->paSegments[iSeg].iSegDef;
|
---|
3240 | if (!omfWriter_PubDefBegin(pThis, pThis->paSegments[iSeg].iGrpDef, idxSegDef))
|
---|
3241 | return false;
|
---|
3242 | for (uint16_t iSym = 0; iSym < cSymbols; iSym++)
|
---|
3243 | if ( pThis->paSymbols[iSym].idxSegDef == idxSegDef
|
---|
3244 | && pThis->paSymbols[iSym].enmType == OMFSYMTYPE_PUBDEF)
|
---|
3245 | {
|
---|
3246 | const char *pszName = &pElfStuff->pchStrTab[paSymbols[iSym].st_name];
|
---|
3247 | if (!omfWriter_PubDefAdd(pThis, paSymbols[iSym].st_value, pszName))
|
---|
3248 | return false;
|
---|
3249 |
|
---|
3250 | /* If the symbol doesn't start with an underscore and is a _c64 or _lm64 symbol,
|
---|
3251 | add an underscore prefixed alias to ease access from 16-bit and 32-bit code. */
|
---|
3252 | size_t cchName = strlen(pszName);
|
---|
3253 | if ( *pszName != '_'
|
---|
3254 | && ( (cchName > 4 && strcmp(&pszName[cchName - 4], "_c64") == 0)
|
---|
3255 | || (cchName > 5 && strcmp(&pszName[cchName - 5], "_lm64") == 0) ) )
|
---|
3256 | {
|
---|
3257 | char szCdeclName[512];
|
---|
3258 | if (cchName > sizeof(szCdeclName) - 2)
|
---|
3259 | cchName = sizeof(szCdeclName) - 2;
|
---|
3260 | szCdeclName[0] = '_';
|
---|
3261 | memcpy(&szCdeclName[1], pszName, cchName);
|
---|
3262 | szCdeclName[cchName + 1] = '\0';
|
---|
3263 | if (!omfWriter_PubDefAdd(pThis, paSymbols[iSym].st_value, szCdeclName))
|
---|
3264 | return false;
|
---|
3265 | }
|
---|
3266 |
|
---|
3267 | pThis->paSymbols[iSym].idx = idxPubDef++;
|
---|
3268 | }
|
---|
3269 | if (!omfWriter_PubDefEnd(pThis))
|
---|
3270 | return false;
|
---|
3271 | }
|
---|
3272 | }
|
---|
3273 |
|
---|
3274 | if (cAbsSyms > 0)
|
---|
3275 | {
|
---|
3276 | if (!omfWriter_PubDefBegin(pThis, 0, 0))
|
---|
3277 | return false;
|
---|
3278 | for (uint16_t iSym = 0; iSym < cSymbols; iSym++)
|
---|
3279 | if ( pThis->paSymbols[iSym].idxSegDef == 0
|
---|
3280 | && pThis->paSymbols[iSym].enmType == OMFSYMTYPE_PUBDEF)
|
---|
3281 | {
|
---|
3282 | const char *pszName = &pElfStuff->pchStrTab[paSymbols[iSym].st_name];
|
---|
3283 | if (!omfWriter_PubDefAdd(pThis, paSymbols[iSym].st_value, pszName))
|
---|
3284 | return false;
|
---|
3285 | pThis->paSymbols[iSym].idx = idxPubDef++;
|
---|
3286 | }
|
---|
3287 | if (!omfWriter_PubDefEnd(pThis))
|
---|
3288 | return false;
|
---|
3289 | }
|
---|
3290 |
|
---|
3291 | /*
|
---|
3292 | * Go over the symbol table and emit external definition records.
|
---|
3293 | */
|
---|
3294 | if (!omfWriter_ExtDefBegin(pThis))
|
---|
3295 | return false;
|
---|
3296 | uint16_t idxExtDef = 1;
|
---|
3297 | for (uint16_t iSym = 0; iSym < cSymbols; iSym++)
|
---|
3298 | if (pThis->paSymbols[iSym].enmType == OMFSYMTYPE_EXTDEF)
|
---|
3299 | {
|
---|
3300 | const char *pszName = &pElfStuff->pchStrTab[paSymbols[iSym].st_name];
|
---|
3301 | if (!omfWriter_ExtDefAdd(pThis, pszName))
|
---|
3302 | return false;
|
---|
3303 | pThis->paSymbols[iSym].idx = idxExtDef++;
|
---|
3304 | }
|
---|
3305 |
|
---|
3306 | if (!omfWriter_ExtDefEnd(pThis))
|
---|
3307 | return false;
|
---|
3308 |
|
---|
3309 | return true;
|
---|
3310 | }
|
---|
3311 |
|
---|
3312 | static bool convertElfSectionsToLeDataAndFixupps(POMFWRITER pThis, PCELFDETAILS pElfStuff, uint8_t const *pbFile, size_t cbFile)
|
---|
3313 | {
|
---|
3314 | Elf64_Sym const *paSymbols = pElfStuff->paSymbols;
|
---|
3315 | Elf64_Shdr const *paShdrs = pElfStuff->paShdrs;
|
---|
3316 | bool fRet = true;
|
---|
3317 | for (uint32_t i = 1; i < pThis->cSegments; i++)
|
---|
3318 | {
|
---|
3319 | if (pThis->paSegments[i].iSegDef == UINT16_MAX)
|
---|
3320 | continue;
|
---|
3321 |
|
---|
3322 | const char *pszSegNm = &pElfStuff->pchShStrTab[paShdrs[i].sh_name];
|
---|
3323 | bool const fRelocs = i + 1 < pThis->cSegments && paShdrs[i + 1].sh_type == SHT_RELA;
|
---|
3324 | uint32_t cRelocs = fRelocs ? paShdrs[i + 1].sh_size / sizeof(Elf64_Rela) : 0;
|
---|
3325 | Elf64_Rela const *paRelocs = fRelocs ? (Elf64_Rela *)&pbFile[paShdrs[i + 1].sh_offset] : NULL;
|
---|
3326 | Elf64_Xword cbVirtData = paShdrs[i].sh_size;
|
---|
3327 | Elf64_Xword cbData = paShdrs[i].sh_type == SHT_NOBITS ? 0 : cbVirtData;
|
---|
3328 | uint8_t const *pbData = &pbFile[paShdrs[i].sh_offset];
|
---|
3329 | uint32_t off = 0;
|
---|
3330 |
|
---|
3331 | /* The OMF record size requires us to split larger sections up. To make
|
---|
3332 | life simple, we fill zeros for unitialized (BSS) stuff. */
|
---|
3333 | const uint32_t cbMaxData = RT_MIN(OMF_MAX_RECORD_PAYLOAD - 1 - (pThis->paSegments[i].iSegDef >= 128) - 4 - 1, _1K);
|
---|
3334 | while (cbVirtData > 0)
|
---|
3335 | {
|
---|
3336 | /* Figure out how many bytes to put out in this chunk. Must make sure
|
---|
3337 | fixups doesn't cross chunk boundraries. ASSUMES sorted relocs. */
|
---|
3338 | uint32_t cChunkRelocs = cRelocs;
|
---|
3339 | uint32_t cbChunk = cbVirtData;
|
---|
3340 | uint32_t offEnd = off + cbChunk;
|
---|
3341 | if (cbChunk > cbMaxData)
|
---|
3342 | {
|
---|
3343 | cbChunk = cbMaxData;
|
---|
3344 | offEnd = off + cbChunk;
|
---|
3345 | cChunkRelocs = 0;
|
---|
3346 |
|
---|
3347 | /* Quickly determin the reloc range. */
|
---|
3348 | while ( cChunkRelocs < cRelocs
|
---|
3349 | && paRelocs[cChunkRelocs].r_offset < offEnd)
|
---|
3350 | cChunkRelocs++;
|
---|
3351 |
|
---|
3352 | /* Ensure final reloc doesn't go beyond chunk. */
|
---|
3353 | while ( cChunkRelocs > 0
|
---|
3354 | && paRelocs[cChunkRelocs - 1].r_offset
|
---|
3355 | + ELF_AMD64_RELOC_SIZE(ELF64_R_TYPE(paRelocs[cChunkRelocs - 1].r_info))
|
---|
3356 | > offEnd)
|
---|
3357 | {
|
---|
3358 | uint32_t cbDrop = offEnd - paRelocs[cChunkRelocs - 1].r_offset;
|
---|
3359 | cbChunk -= cbDrop;
|
---|
3360 | offEnd -= cbDrop;
|
---|
3361 | cChunkRelocs--;
|
---|
3362 | }
|
---|
3363 |
|
---|
3364 | if (!cbVirtData)
|
---|
3365 | return error(pThis->pszSrc, "Wtf? cbVirtData is zero!\n");
|
---|
3366 | }
|
---|
3367 |
|
---|
3368 | /*
|
---|
3369 | * We stash the bytes into the OMF writer record buffer, receiving a
|
---|
3370 | * pointer to the start of it so we can make adjustments if necessary.
|
---|
3371 | */
|
---|
3372 | uint8_t *pbCopy;
|
---|
3373 | if (!omfWriter_LEDataBeginEx(pThis, pThis->paSegments[i].iSegDef, off, cbChunk, cbData, pbData, &pbCopy))
|
---|
3374 | return false;
|
---|
3375 |
|
---|
3376 | /*
|
---|
3377 | * Convert fiuxps.
|
---|
3378 | */
|
---|
3379 | for (uint32_t iReloc = 0; iReloc < cChunkRelocs; iReloc++)
|
---|
3380 | {
|
---|
3381 | /* Get the OMF and ELF data for the symbol the reloc references. */
|
---|
3382 | uint32_t const uType = ELF64_R_TYPE(paRelocs[iReloc].r_info);
|
---|
3383 | uint32_t const iSymbol = ELF64_R_SYM(paRelocs[iReloc].r_info);
|
---|
3384 | Elf64_Sym const * const pElfSym = &paSymbols[iSymbol];
|
---|
3385 | POMFSYMBOL const pOmfSym = &pThis->paSymbols[iSymbol];
|
---|
3386 | const char * const pszSymName = &pElfStuff->pchStrTab[pElfSym->st_name];
|
---|
3387 |
|
---|
3388 | /* Calc fixup location in the pending chunk and setup a flexible pointer to it. */
|
---|
3389 | uint16_t offDataRec = (uint16_t)(paRelocs[iReloc].r_offset - off);
|
---|
3390 | RTPTRUNION uLoc;
|
---|
3391 | uLoc.pu8 = &pbCopy[offDataRec];
|
---|
3392 |
|
---|
3393 | /* OMF fixup data initialized with typical defaults. */
|
---|
3394 | bool fSelfRel = true;
|
---|
3395 | uint8_t bLocation = OMF_FIX_LOC_32BIT_OFFSET;
|
---|
3396 | uint8_t bFrame = OMF_FIX_F_GRPDEF;
|
---|
3397 | uint16_t idxFrame = pThis->idxGrpFlat;
|
---|
3398 | uint8_t bTarget;
|
---|
3399 | uint16_t idxTarget;
|
---|
3400 | bool fTargetDisp;
|
---|
3401 | uint32_t offTargetDisp;
|
---|
3402 | switch (pOmfSym->enmType)
|
---|
3403 | {
|
---|
3404 | case OMFSYMTYPE_INTERNAL:
|
---|
3405 | case OMFSYMTYPE_PUBDEF:
|
---|
3406 | bTarget = OMF_FIX_T_SEGDEF;
|
---|
3407 | idxTarget = pOmfSym->idxSegDef;
|
---|
3408 | fTargetDisp = true;
|
---|
3409 | offTargetDisp = pElfSym->st_value;
|
---|
3410 | break;
|
---|
3411 |
|
---|
3412 | case OMFSYMTYPE_SEGDEF:
|
---|
3413 | bTarget = OMF_FIX_T_SEGDEF_NO_DISP;
|
---|
3414 | idxTarget = pOmfSym->idxSegDef;
|
---|
3415 | fTargetDisp = false;
|
---|
3416 | offTargetDisp = 0;
|
---|
3417 | break;
|
---|
3418 |
|
---|
3419 | case OMFSYMTYPE_EXTDEF:
|
---|
3420 | bTarget = OMF_FIX_T_EXTDEF_NO_DISP;
|
---|
3421 | idxTarget = pOmfSym->idx;
|
---|
3422 | fTargetDisp = false;
|
---|
3423 | offTargetDisp = 0;
|
---|
3424 | break;
|
---|
3425 |
|
---|
3426 | default:
|
---|
3427 | return error(pThis->pszSrc, "Relocation in segment #%u '%s' references ignored or invalid symbol (%s)\n",
|
---|
3428 | i, pszSegNm, pszSymName);
|
---|
3429 | }
|
---|
3430 |
|
---|
3431 | /* Do COFF relocation type conversion. */
|
---|
3432 | switch (uType)
|
---|
3433 | {
|
---|
3434 | case R_X86_64_64:
|
---|
3435 | {
|
---|
3436 | int64_t iAddend = paRelocs[iReloc].r_addend;
|
---|
3437 | if (iAddend > _1G || iAddend < -_1G)
|
---|
3438 | fRet = error(pThis->pszSrc, "R_X86_64_64 with large addend (%" ELF_FMT_D64 ") at %#x in segment #%u '%s'\n",
|
---|
3439 | iAddend, paRelocs[iReloc].r_offset, i, pszSegNm);
|
---|
3440 | *uLoc.pu64 = iAddend;
|
---|
3441 | fSelfRel = false;
|
---|
3442 | break;
|
---|
3443 | }
|
---|
3444 |
|
---|
3445 | case R_X86_64_32:
|
---|
3446 | case R_X86_64_32S: /* signed, unsigned, whatever. */
|
---|
3447 | fSelfRel = false;
|
---|
3448 | /* fall thru */
|
---|
3449 | case R_X86_64_PC32:
|
---|
3450 | {
|
---|
3451 | /* defaults are ok, just handle the addend. */
|
---|
3452 | int32_t iAddend = paRelocs[iReloc].r_addend;
|
---|
3453 | if (iAddend != paRelocs[iReloc].r_addend)
|
---|
3454 | fRet = error(pThis->pszSrc, "R_X86_64_PC32 with large addend (%d) at %#x in segment #%u '%s'\n",
|
---|
3455 | iAddend, paRelocs[iReloc].r_offset, i, pszSegNm);
|
---|
3456 | *uLoc.pu32 = iAddend;
|
---|
3457 | break;
|
---|
3458 | }
|
---|
3459 |
|
---|
3460 | case R_X86_64_NONE:
|
---|
3461 | continue; /* Ignore this one */
|
---|
3462 |
|
---|
3463 | case R_X86_64_GOT32:
|
---|
3464 | case R_X86_64_PLT32:
|
---|
3465 | case R_X86_64_COPY:
|
---|
3466 | case R_X86_64_GLOB_DAT:
|
---|
3467 | case R_X86_64_JMP_SLOT:
|
---|
3468 | case R_X86_64_RELATIVE:
|
---|
3469 | case R_X86_64_GOTPCREL:
|
---|
3470 | case R_X86_64_16:
|
---|
3471 | case R_X86_64_PC16:
|
---|
3472 | case R_X86_64_8:
|
---|
3473 | case R_X86_64_PC8:
|
---|
3474 | case R_X86_64_DTPMOD64:
|
---|
3475 | case R_X86_64_DTPOFF64:
|
---|
3476 | case R_X86_64_TPOFF64:
|
---|
3477 | case R_X86_64_TLSGD:
|
---|
3478 | case R_X86_64_TLSLD:
|
---|
3479 | case R_X86_64_DTPOFF32:
|
---|
3480 | case R_X86_64_GOTTPOFF:
|
---|
3481 | case R_X86_64_TPOFF32:
|
---|
3482 | default:
|
---|
3483 | return error(pThis->pszSrc, "Unsupported fixup type %#x (%s) at rva=%#x in section #%u '%s' against '%s'\n",
|
---|
3484 | uType, g_apszElfAmd64RelTypes[uType], paRelocs[iReloc].r_offset, i, pszSegNm, pszSymName);
|
---|
3485 | }
|
---|
3486 |
|
---|
3487 | /* Add the fixup. */
|
---|
3488 | if (idxFrame == UINT16_MAX)
|
---|
3489 | error(pThis->pszSrc, "idxFrame=UINT16_MAX for %s type=%s\n", pszSymName, g_apszElfAmd64RelTypes[uType]);
|
---|
3490 | fRet = omfWriter_LEDataAddFixup(pThis, offDataRec, fSelfRel, bLocation, bFrame, idxFrame,
|
---|
3491 | bTarget, idxTarget, fTargetDisp, offTargetDisp) && fRet;
|
---|
3492 | }
|
---|
3493 |
|
---|
3494 | /*
|
---|
3495 | * Write the LEDATA and associated FIXUPPs.
|
---|
3496 | */
|
---|
3497 | if (!omfWriter_LEDataEnd(pThis))
|
---|
3498 | return false;
|
---|
3499 |
|
---|
3500 | /*
|
---|
3501 | * Advance.
|
---|
3502 | */
|
---|
3503 | paRelocs += cChunkRelocs;
|
---|
3504 | cRelocs -= cChunkRelocs;
|
---|
3505 | if (cbData > cbChunk)
|
---|
3506 | {
|
---|
3507 | cbData -= cbChunk;
|
---|
3508 | pbData += cbChunk;
|
---|
3509 | }
|
---|
3510 | else
|
---|
3511 | cbData = 0;
|
---|
3512 | off += cbChunk;
|
---|
3513 | cbVirtData -= cbChunk;
|
---|
3514 | }
|
---|
3515 | }
|
---|
3516 |
|
---|
3517 | return fRet;
|
---|
3518 | }
|
---|
3519 |
|
---|
3520 |
|
---|
3521 | static bool convertMachoToOmf(const char *pszFile, uint8_t const *pbFile, size_t cbFile, FILE *pDst)
|
---|
3522 | {
|
---|
3523 | /*
|
---|
3524 | * Validate the source file a little.
|
---|
3525 | */
|
---|
3526 | ELFDETAILS ElfStuff;
|
---|
3527 | if (!validateElf(pszFile, pbFile, cbFile, &ElfStuff))
|
---|
3528 | return false;
|
---|
3529 |
|
---|
3530 | /*
|
---|
3531 | * Instantiate the OMF writer.
|
---|
3532 | */
|
---|
3533 | POMFWRITER pThis = omfWriter_Create(pszFile, ElfStuff.pEhdr->e_shnum, ElfStuff.cSymbols, pDst);
|
---|
3534 | if (!pThis)
|
---|
3535 | return false;
|
---|
3536 |
|
---|
3537 | /*
|
---|
3538 | * Write the OMF object file.
|
---|
3539 | */
|
---|
3540 | if (omfWriter_BeginModule(pThis, pszFile))
|
---|
3541 | {
|
---|
3542 | Elf64_Ehdr const *pEhdr = (Elf64_Ehdr const *)pbFile;
|
---|
3543 | Elf64_Shdr const *paShdrs = (Elf64_Shdr const *)&pbFile[pEhdr->e_shoff];
|
---|
3544 | const char *pszStrTab = (const char *)&pbFile[paShdrs[pEhdr->e_shstrndx].sh_offset];
|
---|
3545 |
|
---|
3546 | if ( convertElfSectionsToSegDefsAndGrpDefs(pThis, &ElfStuff)
|
---|
3547 | && convertElfSymbolsToPubDefsAndExtDefs(pThis, &ElfStuff)
|
---|
3548 | && omfWriter_LinkPassSeparator(pThis)
|
---|
3549 | && convertElfSectionsToLeDataAndFixupps(pThis, &ElfStuff, pbFile, cbFile)
|
---|
3550 | && omfWriter_EndModule(pThis) )
|
---|
3551 | {
|
---|
3552 |
|
---|
3553 | omfWriter_Destroy(pThis);
|
---|
3554 | return true;
|
---|
3555 | }
|
---|
3556 | }
|
---|
3557 |
|
---|
3558 | omfWriter_Destroy(pThis);
|
---|
3559 | return false;
|
---|
3560 | }
|
---|
3561 |
|
---|
3562 | #endif /* !MACHO_TO_OMF_CONVERSION */
|
---|
3563 |
|
---|
3564 |
|
---|
3565 | /*********************************************************************************************************************************
|
---|
3566 | * OMF Converter/Tweaker *
|
---|
3567 | *********************************************************************************************************************************/
|
---|
3568 |
|
---|
3569 | /** Watcom intrinsics we need to modify so we can mix 32-bit and 16-bit
|
---|
3570 | * code, since the 16 and 32 bit compilers share several names.
|
---|
3571 | * The names are length prefixed.
|
---|
3572 | */
|
---|
3573 | static const char * const g_apszExtDefRenames[] =
|
---|
3574 | {
|
---|
3575 | "\x05" "__I4D",
|
---|
3576 | "\x05" "__I4M",
|
---|
3577 | "\x05" "__I8D",
|
---|
3578 | "\x06" "__I8DQ",
|
---|
3579 | "\x07" "__I8DQE",
|
---|
3580 | "\x06" "__I8DR",
|
---|
3581 | "\x07" "__I8DRE",
|
---|
3582 | "\x06" "__I8LS",
|
---|
3583 | "\x05" "__I8M",
|
---|
3584 | "\x06" "__I8ME",
|
---|
3585 | "\x06" "__I8RS",
|
---|
3586 | "\x05" "__PIA",
|
---|
3587 | "\x05" "__PIS",
|
---|
3588 | "\x05" "__PTC",
|
---|
3589 | "\x05" "__PTS",
|
---|
3590 | "\x05" "__U4D",
|
---|
3591 | "\x05" "__U4M",
|
---|
3592 | "\x05" "__U8D",
|
---|
3593 | "\x06" "__U8DQ",
|
---|
3594 | "\x07" "__U8DQE",
|
---|
3595 | "\x06" "__U8DR",
|
---|
3596 | "\x07" "__U8DRE",
|
---|
3597 | "\x06" "__U8LS",
|
---|
3598 | "\x05" "__U8M",
|
---|
3599 | "\x06" "__U8ME",
|
---|
3600 | "\x06" "__U8RS",
|
---|
3601 | };
|
---|
3602 |
|
---|
3603 | /**
|
---|
3604 | * Segment definition.
|
---|
3605 | */
|
---|
3606 | typedef struct OMFSEGDEF
|
---|
3607 | {
|
---|
3608 | uint32_t cbSeg;
|
---|
3609 | uint8_t bSegAttr;
|
---|
3610 | uint16_t idxName;
|
---|
3611 | uint16_t idxClass;
|
---|
3612 | uint16_t idxOverlay;
|
---|
3613 | uint8_t cchName;
|
---|
3614 | uint8_t cchClass;
|
---|
3615 | uint8_t cchOverlay;
|
---|
3616 | const char *pchName;
|
---|
3617 | const char *pchClass;
|
---|
3618 | const char *pchOverlay;
|
---|
3619 | bool fUse32;
|
---|
3620 | } OMFSEGDEF;
|
---|
3621 | typedef OMFSEGDEF *POMFSEGDEF;
|
---|
3622 |
|
---|
3623 | /**
|
---|
3624 | * Group definition.
|
---|
3625 | */
|
---|
3626 | typedef struct OMFGRPDEF
|
---|
3627 | {
|
---|
3628 | const char *pchName;
|
---|
3629 | uint16_t idxName;
|
---|
3630 | uint8_t cchName;
|
---|
3631 | uint16_t cSegDefs;
|
---|
3632 | uint16_t *pidxSegDefs;
|
---|
3633 | } OMFGRPDEF;
|
---|
3634 | typedef OMFGRPDEF *POMFGRPDEF;
|
---|
3635 |
|
---|
3636 | /**
|
---|
3637 | * Records line number information for a file in a segment (for CV8 debug info).
|
---|
3638 | */
|
---|
3639 | typedef struct OMFFILELINES
|
---|
3640 | {
|
---|
3641 | /** The source info offset. */
|
---|
3642 | uint32_t offSrcInfo;
|
---|
3643 | /** Number of line/offset pairs. */
|
---|
3644 | uint32_t cPairs;
|
---|
3645 | /** Number of pairs allocated. */
|
---|
3646 | uint32_t cPairsAlloc;
|
---|
3647 | /** Table with line number and offset pairs, ordered by offset. */
|
---|
3648 | PRTCV8LINEPAIR paPairs;
|
---|
3649 | } OMFFILEINES;
|
---|
3650 | typedef OMFFILEINES *POMFFILEINES;
|
---|
3651 |
|
---|
3652 | /**
|
---|
3653 | * Records line number information for a segment (for CV8 debug info).
|
---|
3654 | */
|
---|
3655 | typedef struct OMFSEGLINES
|
---|
3656 | {
|
---|
3657 | /** Number of files. */
|
---|
3658 | uint32_t cFiles;
|
---|
3659 | /** Number of bytes we need. */
|
---|
3660 | uint32_t cb;
|
---|
3661 | /** The segment index. */
|
---|
3662 | uint16_t idxSeg;
|
---|
3663 | /** The group index for this segment. This is zero if not yet determined and
|
---|
3664 | * UINT16_MAX if no group applicable to this segment. */
|
---|
3665 | uint16_t idxGrp;
|
---|
3666 | /** File table. */
|
---|
3667 | POMFFILEINES paFiles;
|
---|
3668 | } OMFSEGLINES;
|
---|
3669 | typedef OMFSEGLINES *POMFSEGLINES;
|
---|
3670 |
|
---|
3671 | /**
|
---|
3672 | * OMF conversion details.
|
---|
3673 | *
|
---|
3674 | * Keeps information relevant to the conversion and CV8 debug info.
|
---|
3675 | */
|
---|
3676 | typedef struct OMFDETAILS
|
---|
3677 | {
|
---|
3678 | /** Set if it has line numbers. */
|
---|
3679 | bool fLineNumbers;
|
---|
3680 | /** Set if we think this may be a 32-bit OMF file. */
|
---|
3681 | bool fProbably32bit;
|
---|
3682 | /** Set if this module may need mangling. */
|
---|
3683 | bool fMayNeedMangling;
|
---|
3684 | /** Set if this module needs CGROUP16 for line number fixupes. */
|
---|
3685 | bool fNeedsCGroup16;
|
---|
3686 | /** The LNAME index of '$$SYMBOLS' or UINT16_MAX it not found. */
|
---|
3687 | uint16_t iSymbolsNm;
|
---|
3688 | /** The LNAME index of 'DEBSYM' or UINT16_MAX it not found. */
|
---|
3689 | uint16_t iDebSymNm;
|
---|
3690 | /** The '$$SYMBOLS' segment index. */
|
---|
3691 | uint16_t iSymbolsSeg;
|
---|
3692 | /** The LNAME index of 'CGROUP16' or UINT16_MAX it not found. */
|
---|
3693 | uint16_t iCGroup16Nm;
|
---|
3694 | /** The 'CGROUP16' group definition index or UINT16_MAX it not found. */
|
---|
3695 | uint16_t iCGroup16Grp;
|
---|
3696 |
|
---|
3697 | /** Number of SEGDEFs records. */
|
---|
3698 | uint16_t cSegDefs;
|
---|
3699 | /** Number of GRPDEFs records. */
|
---|
3700 | uint16_t cGrpDefs;
|
---|
3701 | /** Number of listed names. */
|
---|
3702 | uint16_t cLNames;
|
---|
3703 |
|
---|
3704 | /** Segment defintions. */
|
---|
3705 | POMFSEGDEF paSegDefs;
|
---|
3706 | /** Group defintions. */
|
---|
3707 | POMFGRPDEF paGrpDefs;
|
---|
3708 | /** Name list. Points to the size repfix. */
|
---|
3709 | char **papchLNames;
|
---|
3710 |
|
---|
3711 | /** CV8: Filename string table size. */
|
---|
3712 | uint32_t cbStrTab;
|
---|
3713 | /** CV8: Filename string table allocation size (always multiple of dword,
|
---|
3714 | * zero initialized). */
|
---|
3715 | uint32_t cbStrTabAlloc;
|
---|
3716 | /** CV8: Filename String table. */
|
---|
3717 | char *pchStrTab;
|
---|
3718 | /** CV8: Elements in the source info table. */
|
---|
3719 | uint16_t cSrcInfo;
|
---|
3720 | /** CV8: Source info table. */
|
---|
3721 | PRTCV8SRCINFO paSrcInfo;
|
---|
3722 |
|
---|
3723 | /** Number of entries in the paSegLines table. */
|
---|
3724 | uint32_t cSegLines;
|
---|
3725 | /** Segment line numbers, indexed by segment number. */
|
---|
3726 | POMFSEGLINES paSegLines;
|
---|
3727 | } OMFDETAILS;
|
---|
3728 | typedef OMFDETAILS *POMFDETAILS;
|
---|
3729 | typedef OMFDETAILS const *PCOMFDETAILS;
|
---|
3730 |
|
---|
3731 | /** Grows a table to a given size (a_cNewEntries). */
|
---|
3732 | #define OMF_GROW_TABLE_EX_RET_ERR(a_EntryType, a_paTable, a_cEntries, a_cNewEntries) \
|
---|
3733 | do\
|
---|
3734 | { \
|
---|
3735 | size_t cbOld = (a_cEntries) * sizeof(a_EntryType); \
|
---|
3736 | size_t cbNew = (a_cNewEntries) * sizeof(a_EntryType); \
|
---|
3737 | void *pvNew = realloc(a_paTable, cbNew); \
|
---|
3738 | if (pvNew) \
|
---|
3739 | { \
|
---|
3740 | memset((uint8_t *)pvNew + cbOld, 0, cbNew - cbOld); \
|
---|
3741 | (a_paTable) = (a_EntryType *)pvNew; \
|
---|
3742 | } \
|
---|
3743 | else return error("???", "Out of memory!\n"); \
|
---|
3744 | } while (0)
|
---|
3745 |
|
---|
3746 | /** Grows a table. */
|
---|
3747 | #define OMF_GROW_TABLE_RET_ERR(a_EntryType, a_paTable, a_cEntries, a_cEvery) \
|
---|
3748 | if ((a_cEntries) % (a_cEvery) != 0) { /* likely */ } \
|
---|
3749 | else do\
|
---|
3750 | { \
|
---|
3751 | size_t cbOld = (a_cEntries) * sizeof(a_EntryType); \
|
---|
3752 | size_t cbNew = cbOld + (a_cEvery) * sizeof(a_EntryType); \
|
---|
3753 | void *pvNew = realloc(a_paTable, cbNew); \
|
---|
3754 | if (pvNew) \
|
---|
3755 | { \
|
---|
3756 | memset((uint8_t *)pvNew + cbOld, 0, (a_cEvery) * sizeof(a_EntryType)); \
|
---|
3757 | (a_paTable) = (a_EntryType *)pvNew; \
|
---|
3758 | } \
|
---|
3759 | else return error("???", "Out of memory!\n"); \
|
---|
3760 | } while (0)
|
---|
3761 |
|
---|
3762 |
|
---|
3763 | /**
|
---|
3764 | * Adds a line number to the CV8 debug info.
|
---|
3765 | *
|
---|
3766 | * @returns success indicator.
|
---|
3767 | * @param pOmfStuff Where to collect CV8 debug info.
|
---|
3768 | * @param cchSrcFile The length of the source file name.
|
---|
3769 | * @param pchSrcFile The source file name, not terminated.
|
---|
3770 | * @param poffFile Where to return the source file information table
|
---|
3771 | * offset (for use in the line number tables).
|
---|
3772 | */
|
---|
3773 | static bool collectOmfAddFile(POMFDETAILS pOmfStuff, uint8_t cchSrcFile, const char *pchSrcFile, uint32_t *poffFile)
|
---|
3774 | {
|
---|
3775 | /*
|
---|
3776 | * Do lookup first.
|
---|
3777 | */
|
---|
3778 | uint32_t i = pOmfStuff->cSrcInfo;
|
---|
3779 | while (i-- > 0)
|
---|
3780 | {
|
---|
3781 | const char *pszCur = &pOmfStuff->pchStrTab[pOmfStuff->paSrcInfo[i].offSourceName];
|
---|
3782 | if ( strncmp(pszCur, pchSrcFile, cchSrcFile) == 0
|
---|
3783 | && pszCur[cchSrcFile] == '\0')
|
---|
3784 | {
|
---|
3785 | *poffFile = i * sizeof(pOmfStuff->paSrcInfo[0]);
|
---|
3786 | return true;
|
---|
3787 | }
|
---|
3788 | }
|
---|
3789 |
|
---|
3790 | /*
|
---|
3791 | * Add it to the string table (dword aligned and zero padded).
|
---|
3792 | */
|
---|
3793 | uint32_t offSrcTab = pOmfStuff->cbStrTab;
|
---|
3794 | if (offSrcTab + cchSrcFile + 1 > pOmfStuff->cbStrTabAlloc)
|
---|
3795 | {
|
---|
3796 | uint32_t cbNew = (offSrcTab == 0) + offSrcTab + cchSrcFile + 1;
|
---|
3797 | cbNew = RT_ALIGN(cbNew, 256);
|
---|
3798 | void *pvNew = realloc(pOmfStuff->pchStrTab, cbNew);
|
---|
3799 | if (!pvNew)
|
---|
3800 | return error("???", "out of memory");
|
---|
3801 | pOmfStuff->pchStrTab = (char *)pvNew;
|
---|
3802 | pOmfStuff->cbStrTabAlloc = cbNew;
|
---|
3803 | memset(&pOmfStuff->pchStrTab[offSrcTab], 0, cbNew - offSrcTab);
|
---|
3804 |
|
---|
3805 | if (!offSrcTab)
|
---|
3806 | offSrcTab++;
|
---|
3807 | }
|
---|
3808 |
|
---|
3809 | memcpy(&pOmfStuff->pchStrTab[offSrcTab], pchSrcFile, cchSrcFile);
|
---|
3810 | pOmfStuff->pchStrTab[offSrcTab + cchSrcFile] = '\0';
|
---|
3811 | pOmfStuff->cbStrTab = offSrcTab + cchSrcFile + 1;
|
---|
3812 |
|
---|
3813 | /*
|
---|
3814 | * Add it to the filename info table.
|
---|
3815 | */
|
---|
3816 | if ((pOmfStuff->cSrcInfo % 8) == 0)
|
---|
3817 | {
|
---|
3818 | void *pvNew = realloc(pOmfStuff->paSrcInfo, sizeof(pOmfStuff->paSrcInfo[0]) * (pOmfStuff->cSrcInfo + 8));
|
---|
3819 | if (!pvNew)
|
---|
3820 | return error("???", "out of memory");
|
---|
3821 | pOmfStuff->paSrcInfo = (PRTCV8SRCINFO)pvNew;
|
---|
3822 | }
|
---|
3823 |
|
---|
3824 | PRTCV8SRCINFO pSrcInfo = &pOmfStuff->paSrcInfo[pOmfStuff->cSrcInfo++];
|
---|
3825 | pSrcInfo->offSourceName = offSrcTab;
|
---|
3826 | pSrcInfo->uDigestType = RTCV8SRCINFO_DIGEST_TYPE_MD5;
|
---|
3827 | memset(&pSrcInfo->Digest, 0, sizeof(pSrcInfo->Digest));
|
---|
3828 |
|
---|
3829 | *poffFile = (uint32_t)((uintptr_t)pSrcInfo - (uintptr_t)pOmfStuff->paSrcInfo);
|
---|
3830 | return true;
|
---|
3831 | }
|
---|
3832 |
|
---|
3833 |
|
---|
3834 | /**
|
---|
3835 | * Adds a line number to the CV8 debug info.
|
---|
3836 | *
|
---|
3837 | * @returns success indicator.
|
---|
3838 | * @param pOmfStuff Where to collect CV8 debug info.
|
---|
3839 | * @param idxSeg The segment index.
|
---|
3840 | * @param off The segment offset.
|
---|
3841 | * @param uLine The line number.
|
---|
3842 | * @param offSrcInfo The source file info table offset.
|
---|
3843 | */
|
---|
3844 | static bool collectOmfAddLine(POMFDETAILS pOmfStuff, uint16_t idxSeg, uint32_t off, uint16_t uLine, uint32_t offSrcInfo)
|
---|
3845 | {
|
---|
3846 | /*
|
---|
3847 | * Get/add the segment line structure.
|
---|
3848 | */
|
---|
3849 | if (idxSeg >= pOmfStuff->cSegLines)
|
---|
3850 | {
|
---|
3851 | OMF_GROW_TABLE_EX_RET_ERR(OMFSEGLINES, pOmfStuff->paSegLines, pOmfStuff->cSegLines, idxSeg + 1);
|
---|
3852 | for (uint32_t i = pOmfStuff->cSegLines; i <= idxSeg; i++)
|
---|
3853 | {
|
---|
3854 | pOmfStuff->paSegLines[i].idxSeg = i;
|
---|
3855 | pOmfStuff->paSegLines[i].idxGrp = UINT16_MAX;
|
---|
3856 | pOmfStuff->paSegLines[i].cb = sizeof(RTCV8LINESHDR);
|
---|
3857 | }
|
---|
3858 | pOmfStuff->cSegLines = idxSeg + 1;
|
---|
3859 | }
|
---|
3860 | POMFSEGLINES pSegLines = &pOmfStuff->paSegLines[idxSeg];
|
---|
3861 |
|
---|
3862 | /*
|
---|
3863 | * Get/add the file structure with the segment.
|
---|
3864 | */
|
---|
3865 | POMFFILEINES pFileLines = NULL;
|
---|
3866 | uint32_t i = pSegLines->cFiles;
|
---|
3867 | while (i-- > 0)
|
---|
3868 | if (pSegLines->paFiles[i].offSrcInfo == offSrcInfo)
|
---|
3869 | {
|
---|
3870 | pFileLines = &pSegLines->paFiles[i];
|
---|
3871 | break;
|
---|
3872 | }
|
---|
3873 | if (!pFileLines)
|
---|
3874 | {
|
---|
3875 | i = pSegLines->cFiles;
|
---|
3876 | OMF_GROW_TABLE_RET_ERR(OMFFILEINES, pSegLines->paFiles, pSegLines->cFiles, 4);
|
---|
3877 | pSegLines->cFiles = i + 1;
|
---|
3878 | pSegLines->cb += sizeof(RTCV8LINESSRCMAP);
|
---|
3879 |
|
---|
3880 | pFileLines = &pSegLines->paFiles[i];
|
---|
3881 | pFileLines->offSrcInfo = offSrcInfo;
|
---|
3882 | pFileLines->cPairs = 0;
|
---|
3883 | pFileLines->cPairsAlloc = 0;
|
---|
3884 | pFileLines->paPairs = NULL;
|
---|
3885 |
|
---|
3886 | /*
|
---|
3887 | * Check for segment group requirements the first time a segment is used.
|
---|
3888 | */
|
---|
3889 | if (i == 0)
|
---|
3890 | {
|
---|
3891 | if (idxSeg >= pOmfStuff->cSegDefs)
|
---|
3892 | return error("???", "collectOmfAddLine: idxSeg=%#x is out of bounds (%#x)!\n", idxSeg, pOmfStuff->cSegDefs);
|
---|
3893 | POMFSEGDEF pSegDef = &pOmfStuff->paSegDefs[idxSeg];
|
---|
3894 | if ( IS_OMF_STR_EQUAL_EX(pSegDef->cchClass, pSegDef->pchClass, "BS3CLASS16CODE")
|
---|
3895 | || IS_OMF_STR_EQUAL_EX(pSegDef->cchClass, pSegDef->pchClass, "CODE"))
|
---|
3896 | {
|
---|
3897 | pOmfStuff->fNeedsCGroup16 = true;
|
---|
3898 | pSegLines->idxGrp = 0; /* We'll replace zeros by the actual CGROUP16 index later. */
|
---|
3899 | }
|
---|
3900 | }
|
---|
3901 | }
|
---|
3902 |
|
---|
3903 | /*
|
---|
3904 | * Add the line number (sorted, duplicates removed).
|
---|
3905 | */
|
---|
3906 | if (pFileLines->cPairs + 1 > pFileLines->cPairsAlloc)
|
---|
3907 | {
|
---|
3908 | void *pvNew = realloc(pFileLines->paPairs, (pFileLines->cPairsAlloc + 16) * sizeof(pFileLines->paPairs[0]));
|
---|
3909 | if (!pvNew)
|
---|
3910 | return error("???", "out of memory");
|
---|
3911 | pFileLines->paPairs = (PRTCV8LINEPAIR)pvNew;
|
---|
3912 | pFileLines->cPairsAlloc += 16;
|
---|
3913 | }
|
---|
3914 |
|
---|
3915 | i = pFileLines->cPairs;
|
---|
3916 | while (i > 0 && ( off < pFileLines->paPairs[i - 1].offSection
|
---|
3917 | || ( off == pFileLines->paPairs[i - 1].offSection
|
---|
3918 | && uLine < pFileLines->paPairs[i - 1].uLineNumber)) )
|
---|
3919 | i--;
|
---|
3920 | if ( i == pFileLines->cPairs
|
---|
3921 | || off != pFileLines->paPairs[i].offSection
|
---|
3922 | || uLine != pFileLines->paPairs[i].uLineNumber)
|
---|
3923 | {
|
---|
3924 | if (i < pFileLines->cPairs)
|
---|
3925 | memmove(&pFileLines->paPairs[i + 1], &pFileLines->paPairs[i],
|
---|
3926 | (pFileLines->cPairs - i) * sizeof(pFileLines->paPairs));
|
---|
3927 | pFileLines->paPairs[i].offSection = off;
|
---|
3928 | pFileLines->paPairs[i].uLineNumber = uLine;
|
---|
3929 | pFileLines->paPairs[i].fEndOfStatement = true;
|
---|
3930 | pFileLines->cPairs++;
|
---|
3931 | pSegLines->cb += sizeof(pFileLines->paPairs[0]);
|
---|
3932 | }
|
---|
3933 |
|
---|
3934 | return true;
|
---|
3935 | }
|
---|
3936 |
|
---|
3937 |
|
---|
3938 | /**
|
---|
3939 | * Parses OMF file gathering line numbers (for CV8 debug info) and checking out
|
---|
3940 | * external defintions for mangling work (compiler instrinsics).
|
---|
3941 | *
|
---|
3942 | * @returns success indicator.
|
---|
3943 | * @param pszFile The name of the OMF file.
|
---|
3944 | * @param pbFile The file content.
|
---|
3945 | * @param cbFile The size of the file content.
|
---|
3946 | * @param pOmfStuff Where to collect CV8 debug info and anything else we
|
---|
3947 | * find out about the OMF file.
|
---|
3948 | */
|
---|
3949 | static bool collectOmfDetails(const char *pszFile, uint8_t const *pbFile, size_t cbFile, POMFDETAILS pOmfStuff)
|
---|
3950 | {
|
---|
3951 | uint32_t cExtDefs = 0;
|
---|
3952 | uint32_t cPubDefs = 0;
|
---|
3953 | uint32_t off = 0;
|
---|
3954 | uint8_t cchSrcFile = 0;
|
---|
3955 | const char *pchSrcFile = NULL;
|
---|
3956 | uint32_t offSrcInfo = UINT32_MAX;
|
---|
3957 |
|
---|
3958 | memset(pOmfStuff, 0, sizeof(*pOmfStuff));
|
---|
3959 | pOmfStuff->iDebSymNm = UINT16_MAX;
|
---|
3960 | pOmfStuff->iSymbolsNm = UINT16_MAX;
|
---|
3961 | pOmfStuff->iSymbolsSeg = UINT16_MAX;
|
---|
3962 | pOmfStuff->iCGroup16Nm = UINT16_MAX;
|
---|
3963 | pOmfStuff->iCGroup16Grp = UINT16_MAX;
|
---|
3964 |
|
---|
3965 | /* Dummy entries. */
|
---|
3966 | OMF_GROW_TABLE_RET_ERR(char *, pOmfStuff->papchLNames, pOmfStuff->cLNames, 16);
|
---|
3967 | pOmfStuff->papchLNames[0] = (char *)"";
|
---|
3968 | pOmfStuff->cLNames = 1;
|
---|
3969 |
|
---|
3970 | OMF_GROW_TABLE_RET_ERR(OMFSEGDEF, pOmfStuff->paSegDefs, pOmfStuff->cSegDefs, 16);
|
---|
3971 | pOmfStuff->cSegDefs = 1;
|
---|
3972 |
|
---|
3973 | OMF_GROW_TABLE_RET_ERR(OMFGRPDEF, pOmfStuff->paGrpDefs, pOmfStuff->cGrpDefs, 16);
|
---|
3974 | pOmfStuff->cGrpDefs = 1;
|
---|
3975 |
|
---|
3976 | /*
|
---|
3977 | * Process the OMF records.
|
---|
3978 | */
|
---|
3979 | while (off + 3 < cbFile)
|
---|
3980 | {
|
---|
3981 | uint8_t bRecType = pbFile[off];
|
---|
3982 | uint16_t cbRec = RT_MAKE_U16(pbFile[off + 1], pbFile[off + 2]);
|
---|
3983 | if (g_cVerbose > 2)
|
---|
3984 | printf( "%#07x: type=%#04x len=%#06x\n", off, bRecType, cbRec);
|
---|
3985 | if (off + cbRec > cbFile)
|
---|
3986 | return error(pszFile, "Invalid record length at %#x: %#x (cbFile=%#lx)\n", off, cbRec, (unsigned long)cbFile);
|
---|
3987 |
|
---|
3988 | uint32_t offRec = 0;
|
---|
3989 | uint8_t const *pbRec = &pbFile[off + 3];
|
---|
3990 | #define OMF_CHECK_RET(a_cbReq, a_Name) /* Not taking the checksum into account, so we're good with 1 or 2 byte fields. */ \
|
---|
3991 | if (offRec + (a_cbReq) <= cbRec) {/*likely*/} \
|
---|
3992 | else return error(pszFile, "Malformed " #a_Name "! off=%#x offRec=%#x cbRec=%#x cbNeeded=%#x line=%d\n", \
|
---|
3993 | off, offRec, cbRec, (a_cbReq), __LINE__)
|
---|
3994 | #define OMF_READ_IDX(a_idx, a_Name) \
|
---|
3995 | do { \
|
---|
3996 | OMF_CHECK_RET(2, a_Name); \
|
---|
3997 | a_idx = pbRec[offRec++]; \
|
---|
3998 | if ((a_idx) & 0x80) \
|
---|
3999 | a_idx = (((a_idx) & 0x7f) << 8) | pbRec[offRec++]; \
|
---|
4000 | } while (0)
|
---|
4001 |
|
---|
4002 | #define OMF_READ_U16(a_u16, a_Name) \
|
---|
4003 | do { \
|
---|
4004 | OMF_CHECK_RET(4, a_Name); \
|
---|
4005 | a_u16 = RT_MAKE_U16(pbRec[offRec], pbRec[offRec + 1]); \
|
---|
4006 | offRec += 2; \
|
---|
4007 | } while (0)
|
---|
4008 | #define OMF_READ_U32(a_u32, a_Name) \
|
---|
4009 | do { \
|
---|
4010 | OMF_CHECK_RET(4, a_Name); \
|
---|
4011 | a_u32 = RT_MAKE_U32_FROM_U8(pbRec[offRec], pbRec[offRec + 1], pbRec[offRec + 2], pbRec[offRec + 3]); \
|
---|
4012 | offRec += 4; \
|
---|
4013 | } while (0)
|
---|
4014 | #define OMF_EXPLODE_LNAME(a_idxName, a_pchName, a_cchName, a_Name) \
|
---|
4015 | do { \
|
---|
4016 | if ((a_idxName) < pOmfStuff->cLNames) \
|
---|
4017 | { \
|
---|
4018 | a_cchName = (uint8_t)*pOmfStuff->papchLNames[(a_idxName)]; \
|
---|
4019 | a_pchName = pOmfStuff->papchLNames[(a_idxName)] + 1; \
|
---|
4020 | } \
|
---|
4021 | else return error(pszFile, "Invalid LNAME reference %#x in " #a_Name "!\n", a_idxName); \
|
---|
4022 | } while (0)
|
---|
4023 |
|
---|
4024 | switch (bRecType)
|
---|
4025 | {
|
---|
4026 | /*
|
---|
4027 | * Record LNAME records, scanning for FLAT.
|
---|
4028 | */
|
---|
4029 | case OMF_LNAMES:
|
---|
4030 | while (offRec + 1 < cbRec)
|
---|
4031 | {
|
---|
4032 | uint8_t cch = pbRec[offRec];
|
---|
4033 | if (offRec + 1 + cch >= cbRec)
|
---|
4034 | return error(pszFile, "Invalid LNAME string length at %#x+3+%#x: %#x (cbFile=%#lx)\n",
|
---|
4035 | off, offRec, cch, (unsigned long)cbFile);
|
---|
4036 |
|
---|
4037 | if (g_cVerbose > 2)
|
---|
4038 | printf(" LNAME[%u]: %-*.*s\n", pOmfStuff->cLNames, cch, cch, &pbRec[offRec + 1]);
|
---|
4039 |
|
---|
4040 | OMF_GROW_TABLE_RET_ERR(char *, pOmfStuff->papchLNames, pOmfStuff->cLNames, 16);
|
---|
4041 | pOmfStuff->papchLNames[pOmfStuff->cLNames] = (char *)&pbRec[offRec];
|
---|
4042 |
|
---|
4043 | if (IS_OMF_STR_EQUAL_EX(cch, &pbRec[offRec + 1], "FLAT"))
|
---|
4044 | pOmfStuff->fProbably32bit = true;
|
---|
4045 |
|
---|
4046 | if (IS_OMF_STR_EQUAL_EX(cch, &pbRec[offRec + 1], "DEBSYM"))
|
---|
4047 | pOmfStuff->iDebSymNm = pOmfStuff->cLNames;
|
---|
4048 | if (IS_OMF_STR_EQUAL_EX(cch, &pbRec[offRec + 1], "$$SYMBOLS"))
|
---|
4049 | pOmfStuff->iSymbolsNm = pOmfStuff->cLNames;
|
---|
4050 | if (IS_OMF_STR_EQUAL_EX(cch, &pbRec[offRec + 1], "CGROUP16"))
|
---|
4051 | pOmfStuff->iCGroup16Nm = pOmfStuff->cLNames;
|
---|
4052 |
|
---|
4053 | pOmfStuff->cLNames++;
|
---|
4054 | offRec += cch + 1;
|
---|
4055 | }
|
---|
4056 | break;
|
---|
4057 |
|
---|
4058 | /*
|
---|
4059 | * Display external definitions if -v is specified, also check if anything needs mangling.
|
---|
4060 | */
|
---|
4061 | case OMF_EXTDEF:
|
---|
4062 | while (offRec + 1 < cbRec)
|
---|
4063 | {
|
---|
4064 | uint8_t cch = pbRec[offRec++];
|
---|
4065 | OMF_CHECK_RET(cch, EXTDEF);
|
---|
4066 | char *pchName = (char *)&pbRec[offRec];
|
---|
4067 | offRec += cch;
|
---|
4068 |
|
---|
4069 | uint16_t idxType;
|
---|
4070 | OMF_READ_IDX(idxType, EXTDEF);
|
---|
4071 |
|
---|
4072 | if (g_cVerbose > 2)
|
---|
4073 | printf(" EXTDEF [%u]: %-*.*s type=%#x\n", cExtDefs, cch, cch, pchName, idxType);
|
---|
4074 | else if (g_cVerbose > 0)
|
---|
4075 | printf(" U %-*.*s\n", cch, cch, pchName);
|
---|
4076 |
|
---|
4077 | /* Look for g_apszExtDefRenames entries that requires changing. */
|
---|
4078 | if ( !pOmfStuff->fMayNeedMangling
|
---|
4079 | && cch >= 5
|
---|
4080 | && cch <= 7
|
---|
4081 | && pchName[0] == '_'
|
---|
4082 | && pchName[1] == '_'
|
---|
4083 | && ( pchName[2] == 'U'
|
---|
4084 | || pchName[2] == 'I'
|
---|
4085 | || pchName[2] == 'P')
|
---|
4086 | && ( pchName[3] == '4'
|
---|
4087 | || pchName[3] == '8'
|
---|
4088 | || pchName[3] == 'I'
|
---|
4089 | || pchName[3] == 'T') )
|
---|
4090 | {
|
---|
4091 | pOmfStuff->fMayNeedMangling = true;
|
---|
4092 | }
|
---|
4093 | }
|
---|
4094 | break;
|
---|
4095 |
|
---|
4096 | /*
|
---|
4097 | * Display public names if -v is specified.
|
---|
4098 | */
|
---|
4099 | case OMF_PUBDEF32:
|
---|
4100 | case OMF_LPUBDEF32:
|
---|
4101 | pOmfStuff->fProbably32bit = true;
|
---|
4102 | case OMF_PUBDEF16:
|
---|
4103 | case OMF_LPUBDEF16:
|
---|
4104 | if (g_cVerbose > 0)
|
---|
4105 | {
|
---|
4106 | char const chType = bRecType == OMF_PUBDEF16 || bRecType == OMF_PUBDEF32 ? 'T' : 't';
|
---|
4107 | const char *pszRec = "LPUBDEF";
|
---|
4108 | if (chType == 'T')
|
---|
4109 | pszRec++;
|
---|
4110 |
|
---|
4111 | uint16_t idxGrp;
|
---|
4112 | OMF_READ_IDX(idxGrp, [L]PUBDEF);
|
---|
4113 |
|
---|
4114 | uint16_t idxSeg;
|
---|
4115 | OMF_READ_IDX(idxSeg, [L]PUBDEF);
|
---|
4116 |
|
---|
4117 | uint16_t uFrameBase = 0;
|
---|
4118 | if (idxSeg == 0)
|
---|
4119 | {
|
---|
4120 | OMF_CHECK_RET(2, [L]PUBDEF);
|
---|
4121 | uFrameBase = RT_MAKE_U16(pbRec[offRec], pbRec[offRec + 1]);
|
---|
4122 | offRec += 2;
|
---|
4123 | }
|
---|
4124 | if (g_cVerbose > 2)
|
---|
4125 | printf(" %s: idxGrp=%#x idxSeg=%#x uFrameBase=%#x\n", pszRec, idxGrp, idxSeg, uFrameBase);
|
---|
4126 | uint16_t const uSeg = idxSeg ? idxSeg : uFrameBase;
|
---|
4127 |
|
---|
4128 | while (offRec + 1 < cbRec)
|
---|
4129 | {
|
---|
4130 | uint8_t cch = pbRec[offRec++];
|
---|
4131 | OMF_CHECK_RET(cch, [L]PUBDEF);
|
---|
4132 | const char *pchName = (const char *)&pbRec[offRec];
|
---|
4133 | offRec += cch;
|
---|
4134 |
|
---|
4135 | uint32_t offSeg;
|
---|
4136 | if (bRecType & OMF_REC32)
|
---|
4137 | {
|
---|
4138 | OMF_CHECK_RET(4, [L]PUBDEF);
|
---|
4139 | offSeg = RT_MAKE_U32_FROM_U8(pbRec[offRec], pbRec[offRec + 1], pbRec[offRec + 2], pbRec[offRec + 3]);
|
---|
4140 | offRec += 4;
|
---|
4141 | }
|
---|
4142 | else
|
---|
4143 | {
|
---|
4144 | OMF_CHECK_RET(2, [L]PUBDEF);
|
---|
4145 | offSeg = RT_MAKE_U16(pbRec[offRec], pbRec[offRec + 1]);
|
---|
4146 | offRec += 2;
|
---|
4147 | }
|
---|
4148 |
|
---|
4149 | uint16_t idxType;
|
---|
4150 | OMF_READ_IDX(idxType, [L]PUBDEF);
|
---|
4151 |
|
---|
4152 | if (g_cVerbose > 2)
|
---|
4153 | printf(" %s[%u]: off=%#010x type=%#x %-*.*s\n", pszRec, cPubDefs, offSeg, idxType, cch, cch, pchName);
|
---|
4154 | else if (g_cVerbose > 0)
|
---|
4155 | printf("%04x:%08x %c %-*.*s\n", uSeg, offSeg, chType, cch, cch, pchName);
|
---|
4156 | }
|
---|
4157 | }
|
---|
4158 | break;
|
---|
4159 |
|
---|
4160 | /*
|
---|
4161 | * Must count segment definitions to figure the index of our segment.
|
---|
4162 | */
|
---|
4163 | case OMF_SEGDEF16:
|
---|
4164 | case OMF_SEGDEF32:
|
---|
4165 | {
|
---|
4166 | OMF_GROW_TABLE_RET_ERR(OMFSEGDEF, pOmfStuff->paSegDefs, pOmfStuff->cSegDefs, 16);
|
---|
4167 | POMFSEGDEF pSegDef = &pOmfStuff->paSegDefs[pOmfStuff->cSegDefs++];
|
---|
4168 |
|
---|
4169 | OMF_CHECK_RET(1 + (bRecType == OMF_SEGDEF16 ? 2 : 4) + 1 + 1 + 1, SEGDEF);
|
---|
4170 | pSegDef->bSegAttr = pbRec[offRec++];
|
---|
4171 | pSegDef->fUse32 = pSegDef->bSegAttr & 1;
|
---|
4172 | if ((pSegDef->bSegAttr >> 5) == 0)
|
---|
4173 | {
|
---|
4174 | /* A=0: skip frame number of offset. */
|
---|
4175 | OMF_CHECK_RET(3, SEGDEF);
|
---|
4176 | offRec += 3;
|
---|
4177 | }
|
---|
4178 | if (bRecType == OMF_SEGDEF16)
|
---|
4179 | OMF_READ_U16(pSegDef->cbSeg, SEGDEF16);
|
---|
4180 | else
|
---|
4181 | OMF_READ_U32(pSegDef->cbSeg, SEGDEF32);
|
---|
4182 | OMF_READ_IDX(pSegDef->idxName, SEGDEF);
|
---|
4183 | OMF_READ_IDX(pSegDef->idxClass, SEGDEF);
|
---|
4184 | OMF_READ_IDX(pSegDef->idxOverlay, SEGDEF);
|
---|
4185 | OMF_EXPLODE_LNAME(pSegDef->idxName, pSegDef->pchName, pSegDef->cchName, SEGDEF);
|
---|
4186 | OMF_EXPLODE_LNAME(pSegDef->idxClass, pSegDef->pchClass, pSegDef->cchClass, SEGDEF);
|
---|
4187 | OMF_EXPLODE_LNAME(pSegDef->idxOverlay, pSegDef->pchOverlay, pSegDef->cchOverlay, SEGDEF);
|
---|
4188 | break;
|
---|
4189 | }
|
---|
4190 |
|
---|
4191 | /*
|
---|
4192 | * Must count segment definitions to figure the index of our segment.
|
---|
4193 | */
|
---|
4194 | case OMF_GRPDEF:
|
---|
4195 | {
|
---|
4196 | OMF_GROW_TABLE_RET_ERR(OMFGRPDEF, pOmfStuff->paGrpDefs, pOmfStuff->cGrpDefs, 8);
|
---|
4197 | POMFGRPDEF pGrpDef = &pOmfStuff->paGrpDefs[pOmfStuff->cGrpDefs];
|
---|
4198 |
|
---|
4199 | OMF_READ_IDX(pGrpDef->idxName, GRPDEF);
|
---|
4200 | OMF_EXPLODE_LNAME(pGrpDef->idxName, pGrpDef->pchName, pGrpDef->cchName, GRPDEF);
|
---|
4201 | if (pGrpDef->idxName == pOmfStuff->iCGroup16Grp)
|
---|
4202 | pOmfStuff->iCGroup16Grp = pOmfStuff->cGrpDefs;
|
---|
4203 |
|
---|
4204 | pGrpDef->cSegDefs = 0;
|
---|
4205 | pGrpDef->pidxSegDefs = NULL;
|
---|
4206 | while (offRec + 2 + 1 < cbRec)
|
---|
4207 | {
|
---|
4208 | if (pbRec[offRec] != 0xff)
|
---|
4209 | return error(pszFile, "Unsupported GRPDEF member type: %#x\n", pbRec[offRec]);
|
---|
4210 | offRec++;
|
---|
4211 | OMF_GROW_TABLE_RET_ERR(uint16_t, pGrpDef->pidxSegDefs, pGrpDef->cSegDefs, 16);
|
---|
4212 | OMF_READ_IDX(pGrpDef->pidxSegDefs[pGrpDef->cSegDefs], GRPDEF);
|
---|
4213 | pGrpDef->cSegDefs++;
|
---|
4214 | }
|
---|
4215 | pOmfStuff->cGrpDefs++;
|
---|
4216 | break;
|
---|
4217 | }
|
---|
4218 |
|
---|
4219 | /*
|
---|
4220 | * Gather file names.
|
---|
4221 | */
|
---|
4222 | case OMF_THEADR: /* watcom */
|
---|
4223 | cchSrcFile = pbRec[offRec++];
|
---|
4224 | OMF_CHECK_RET(cchSrcFile, OMF_THEADR);
|
---|
4225 | pchSrcFile = (const char *)&pbRec[offRec];
|
---|
4226 | if (!collectOmfAddFile(pOmfStuff, cchSrcFile, pchSrcFile, &offSrcInfo))
|
---|
4227 | return false;
|
---|
4228 | break;
|
---|
4229 |
|
---|
4230 | case OMF_COMENT:
|
---|
4231 | {
|
---|
4232 | OMF_CHECK_RET(2, COMENT);
|
---|
4233 | offRec++; /* skip the type (flags) */
|
---|
4234 | uint8_t bClass = pbRec[offRec++];
|
---|
4235 | if (bClass == OMF_CCLS_BORLAND_SRC_FILE) /* nasm */
|
---|
4236 | {
|
---|
4237 | OMF_CHECK_RET(1+1+4, BORLAND_SRC_FILE);
|
---|
4238 | offRec++; /* skip unknown byte */
|
---|
4239 | cchSrcFile = pbRec[offRec++];
|
---|
4240 | OMF_CHECK_RET(cchSrcFile + 4, BORLAND_SRC_FILE);
|
---|
4241 | pchSrcFile = (const char *)&pbRec[offRec];
|
---|
4242 | offRec += cchSrcFile;
|
---|
4243 | if (offRec + 4 + 1 != cbRec)
|
---|
4244 | return error(pszFile, "BAD BORLAND_SRC_FILE record at %#x: %d bytes left\n",
|
---|
4245 | off, cbRec - offRec - 4 - 1);
|
---|
4246 | if (!collectOmfAddFile(pOmfStuff, cchSrcFile, pchSrcFile, &offSrcInfo))
|
---|
4247 | return false;
|
---|
4248 | break;
|
---|
4249 | }
|
---|
4250 | break;
|
---|
4251 | }
|
---|
4252 |
|
---|
4253 | /*
|
---|
4254 | * Line number conversion.
|
---|
4255 | */
|
---|
4256 | case OMF_LINNUM16:
|
---|
4257 | case OMF_LINNUM32:
|
---|
4258 | {
|
---|
4259 | uint16_t idxGrp;
|
---|
4260 | OMF_READ_IDX(idxGrp, LINNUM);
|
---|
4261 | uint16_t idxSeg;
|
---|
4262 | OMF_READ_IDX(idxSeg, LINNUM);
|
---|
4263 |
|
---|
4264 | uint16_t iLine;
|
---|
4265 | uint32_t offSeg;
|
---|
4266 | if (bRecType == OMF_LINNUM16)
|
---|
4267 | while (offRec + 4 < cbRec)
|
---|
4268 | {
|
---|
4269 | iLine = RT_MAKE_U16(pbRec[offRec + 0], pbRec[offRec + 1]);
|
---|
4270 | offSeg = RT_MAKE_U16(pbRec[offRec + 2], pbRec[offRec + 3]);
|
---|
4271 | if (!collectOmfAddLine(pOmfStuff, idxSeg, offSeg, iLine, offSrcInfo))
|
---|
4272 | return false;
|
---|
4273 | offRec += 4;
|
---|
4274 | }
|
---|
4275 | else
|
---|
4276 | while (offRec + 6 < cbRec)
|
---|
4277 | {
|
---|
4278 | iLine = RT_MAKE_U16(pbRec[offRec + 0], pbRec[offRec + 1]);
|
---|
4279 | offSeg = RT_MAKE_U32_FROM_U8(pbRec[offRec + 2], pbRec[offRec + 3], pbRec[offRec + 4], pbRec[offRec + 5]);
|
---|
4280 | if (!collectOmfAddLine(pOmfStuff, idxSeg, offSeg, iLine, offSrcInfo))
|
---|
4281 | return false;
|
---|
4282 | offRec += 6;
|
---|
4283 | }
|
---|
4284 | if (offRec + 1 != cbRec)
|
---|
4285 | return error(pszFile, "BAD LINNUM record at %#x: %d bytes left\n", off, cbRec - offRec - 1);
|
---|
4286 | break;
|
---|
4287 | }
|
---|
4288 | }
|
---|
4289 |
|
---|
4290 | /* advance */
|
---|
4291 | off += cbRec + 3;
|
---|
4292 | }
|
---|
4293 |
|
---|
4294 | return true;
|
---|
4295 | #undef OMF_READ_IDX
|
---|
4296 | #undef OMF_CHECK_RET
|
---|
4297 | }
|
---|
4298 |
|
---|
4299 |
|
---|
4300 | /**
|
---|
4301 | * Writes the debug segment definitions (names too).
|
---|
4302 | *
|
---|
4303 | * @returns success indicator.
|
---|
4304 | * @param pThis The OMF writer.
|
---|
4305 | * @param pOmfStuff The OMF stuff with CV8 line number info.
|
---|
4306 | */
|
---|
4307 | static bool convertOmfWriteDebugSegDefs(POMFWRITER pThis, POMFDETAILS pOmfStuff)
|
---|
4308 | {
|
---|
4309 | if ( pOmfStuff->cSegLines == 0
|
---|
4310 | || pOmfStuff->iSymbolsSeg != UINT16_MAX)
|
---|
4311 | return true;
|
---|
4312 |
|
---|
4313 | /*
|
---|
4314 | * Emit the LNAMES we need.
|
---|
4315 | */
|
---|
4316 | #if 1
|
---|
4317 | if ( pOmfStuff->iSymbolsNm == UINT16_MAX
|
---|
4318 | || pOmfStuff->iDebSymNm == UINT16_MAX)
|
---|
4319 | {
|
---|
4320 | if ( !omfWriter_LNamesBegin(pThis, true /*fAddZeroEntry*/)
|
---|
4321 | || ( pOmfStuff->iSymbolsNm == UINT16_MAX
|
---|
4322 | && !omfWriter_LNamesAdd(pThis, "$$SYMBOLS", &pOmfStuff->iSymbolsNm))
|
---|
4323 | || ( pOmfStuff->iDebSymNm == UINT16_MAX
|
---|
4324 | && !omfWriter_LNamesAdd(pThis, "DEBSYM", &pOmfStuff->iDebSymNm))
|
---|
4325 | || !omfWriter_LNamesEnd(pThis) )
|
---|
4326 | return false;
|
---|
4327 | }
|
---|
4328 | #else
|
---|
4329 | if ( !omfWriter_LNamesBegin(pThis, true /*fAddZeroEntry*/)
|
---|
4330 | || !omfWriter_LNamesAdd(pThis, "$$SYMBOLS2", &pOmfStuff->iSymbolsNm)
|
---|
4331 | || !omfWriter_LNamesAdd(pThis, "DEBSYM2", &pOmfStuff->iDebSymNm)
|
---|
4332 | || !omfWriter_LNamesEnd(pThis) )
|
---|
4333 | return false;
|
---|
4334 | #endif
|
---|
4335 |
|
---|
4336 | /*
|
---|
4337 | * Emit the segment definitions.
|
---|
4338 | */
|
---|
4339 | pOmfStuff->iSymbolsSeg = pOmfStuff->cSegDefs++;
|
---|
4340 |
|
---|
4341 | uint8_t bSegAttr = 0;
|
---|
4342 | bSegAttr |= 5 << 5; /* A: dword alignment */
|
---|
4343 | bSegAttr |= 0 << 2; /* C: private */
|
---|
4344 | bSegAttr |= 0 << 1; /* B: not big */
|
---|
4345 | bSegAttr |= 1; /* D: use32 */
|
---|
4346 |
|
---|
4347 | /* calc the segment size. */
|
---|
4348 | uint32_t cbSeg = 4; /* dword 4 */
|
---|
4349 | cbSeg += 4 + 4 + RT_ALIGN_32(pOmfStuff->cbStrTab, 4);
|
---|
4350 | cbSeg += 4 + 4 + pOmfStuff->cSrcInfo * sizeof(pOmfStuff->paSrcInfo[0]);
|
---|
4351 | uint32_t i = pOmfStuff->cSegLines;
|
---|
4352 | while (i-- > 0)
|
---|
4353 | if (pOmfStuff->paSegLines[i].cFiles > 0)
|
---|
4354 | cbSeg += 4 + 4 + pOmfStuff->paSegLines[i].cb;
|
---|
4355 | return omfWriter_SegDef(pThis, bSegAttr, cbSeg, pOmfStuff->iSymbolsNm, pOmfStuff->iDebSymNm);
|
---|
4356 | }
|
---|
4357 |
|
---|
4358 |
|
---|
4359 | /**
|
---|
4360 | * Writes additional segment group definitions.
|
---|
4361 | *
|
---|
4362 | * @returns success indicator.
|
---|
4363 | * @param pThis The OMF writer.
|
---|
4364 | * @param pOmfStuff The OMF stuff with CV8 line number info.
|
---|
4365 | */
|
---|
4366 | static bool convertOmfWriteDebugGrpDefs(POMFWRITER pThis, POMFDETAILS pOmfStuff)
|
---|
4367 | {
|
---|
4368 | if ( pOmfStuff->cSegLines == 0
|
---|
4369 | || !pOmfStuff->fNeedsCGroup16)
|
---|
4370 | return true;
|
---|
4371 |
|
---|
4372 | if (pOmfStuff->iCGroup16Grp == UINT16_MAX)
|
---|
4373 | {
|
---|
4374 | if (pOmfStuff->iCGroup16Nm == UINT16_MAX)
|
---|
4375 | if ( !omfWriter_LNamesBegin(pThis, true)
|
---|
4376 | || !omfWriter_LNamesAdd(pThis, "CGROUP16", &pOmfStuff->iCGroup16Nm)
|
---|
4377 | || !omfWriter_LNamesEnd(pThis))
|
---|
4378 | return false;
|
---|
4379 |
|
---|
4380 | if ( !omfWriter_GrpDefBegin(pThis, pOmfStuff->iCGroup16Nm)
|
---|
4381 | || !omfWriter_GrpDefEnd(pThis))
|
---|
4382 | return false;
|
---|
4383 | pOmfStuff->iCGroup16Grp = pOmfStuff->cGrpDefs;
|
---|
4384 | }
|
---|
4385 |
|
---|
4386 | for (unsigned i = 0; i < pOmfStuff->cSegLines; i++)
|
---|
4387 | if (pOmfStuff->paSegLines[i].idxGrp == 0)
|
---|
4388 | pOmfStuff->paSegLines[i].idxGrp = pOmfStuff->iCGroup16Grp;
|
---|
4389 | return true;
|
---|
4390 | }
|
---|
4391 |
|
---|
4392 |
|
---|
4393 | /**
|
---|
4394 | * Writes the debug segment data.
|
---|
4395 | *
|
---|
4396 | * @returns success indicator.
|
---|
4397 | * @param pThis The OMF writer.
|
---|
4398 | * @param pOmfStuff The OMF stuff with CV8 line number info.
|
---|
4399 | */
|
---|
4400 | static bool convertOmfWriteDebugData(POMFWRITER pThis, POMFDETAILS pOmfStuff)
|
---|
4401 | {
|
---|
4402 | if (pOmfStuff->cSegLines == 0)
|
---|
4403 | return true;
|
---|
4404 |
|
---|
4405 | /* Begin and write the CV version signature. */
|
---|
4406 | uint32_t const cbMaxChunk = RT_ALIGN(OMF_MAX_RECORD_PAYLOAD - 1 - 16, 4); /* keep the data dword aligned */
|
---|
4407 | if ( !omfWriter_LEDataBegin(pThis, pOmfStuff->iSymbolsSeg, 0)
|
---|
4408 | || !omfWriter_LEDataAddU32(pThis, RTCVSYMBOLS_SIGNATURE_CV8))
|
---|
4409 | return false;
|
---|
4410 |
|
---|
4411 | /*
|
---|
4412 | * Emit the string table (no fixups).
|
---|
4413 | */
|
---|
4414 | uint32_t cbLeft = pOmfStuff->cbStrTab;
|
---|
4415 | if ( !omfWriter_LEDataAddU32(pThis, RTCV8SYMBLOCK_TYPE_SRC_STR)
|
---|
4416 | || !omfWriter_LEDataAddU32(pThis, cbLeft)
|
---|
4417 | || !omfWriter_LEDataAddBytes(pThis, pOmfStuff->pchStrTab, RT_ALIGN_32(cbLeft, 4)) ) /* table is zero padded to nearest dword */
|
---|
4418 | return false;
|
---|
4419 |
|
---|
4420 | /*
|
---|
4421 | * Emit the source file info table (no fixups).
|
---|
4422 | */
|
---|
4423 | cbLeft = pOmfStuff->cSrcInfo * sizeof(pOmfStuff->paSrcInfo[0]);
|
---|
4424 | if ( !omfWriter_LEDataAddU32(pThis, RTCV8SYMBLOCK_TYPE_SRC_INFO)
|
---|
4425 | || !omfWriter_LEDataAddU32(pThis, cbLeft)
|
---|
4426 | || !omfWriter_LEDataAddBytes(pThis, pOmfStuff->paSrcInfo, cbLeft) )
|
---|
4427 | return false;
|
---|
4428 |
|
---|
4429 | /*
|
---|
4430 | * Emit the segment line numbers. There are two fixups here at the start
|
---|
4431 | * of each chunk.
|
---|
4432 | */
|
---|
4433 | POMFSEGLINES pSegLines = pOmfStuff->paSegLines;
|
---|
4434 | uint32_t i = pOmfStuff->cSegLines;
|
---|
4435 | while (i-- > 0)
|
---|
4436 | {
|
---|
4437 | if (pSegLines->cFiles)
|
---|
4438 | {
|
---|
4439 | /* Calc covered area. */
|
---|
4440 | uint32_t cbSectionCovered = 0;
|
---|
4441 | uint32_t j = pSegLines->cFiles;
|
---|
4442 | while (j-- > 0)
|
---|
4443 | {
|
---|
4444 | uint32_t offLast = pSegLines->paFiles[j].paPairs[pSegLines->paFiles[j].cPairs - 1].offSection;
|
---|
4445 | if (offLast > cbSectionCovered)
|
---|
4446 | offLast = cbSectionCovered;
|
---|
4447 | }
|
---|
4448 |
|
---|
4449 | /* For simplicity and debuggability, just split the LEDATA here. */
|
---|
4450 | if ( !omfWriter_LEDataSplit(pThis)
|
---|
4451 | || !omfWriter_LEDataAddU32(pThis, RTCV8SYMBLOCK_TYPE_SECT_LINES)
|
---|
4452 | || !omfWriter_LEDataAddU32(pThis, pSegLines->cb)
|
---|
4453 | || !omfWriter_LEDataAddU32(pThis, 0) /*RTCV8LINESHDR::offSection*/
|
---|
4454 | || !omfWriter_LEDataAddU16(pThis, 0) /*RTCV8LINESHDR::iSection*/
|
---|
4455 | || !omfWriter_LEDataAddU16(pThis, 0) /*RTCV8LINESHDR::u16Padding*/
|
---|
4456 | || !omfWriter_LEDataAddU32(pThis, cbSectionCovered) /*RTCV8LINESHDR::cbSectionCovered*/ )
|
---|
4457 | return false;
|
---|
4458 |
|
---|
4459 | /* Default to the segment (BS3TEXT32, BS3TEXT64) or the group (CGROUP16,
|
---|
4460 | RMGROUP16, etc). The important thing is that we're framing the fixups
|
---|
4461 | using a segment or group which ends up in the codeview segment map. */
|
---|
4462 | uint16_t idxFrame = pSegLines->idxSeg;
|
---|
4463 | uint8_t bFrame = OMF_FIX_F_SEGDEF;
|
---|
4464 | if (pSegLines->idxGrp != UINT16_MAX)
|
---|
4465 | {
|
---|
4466 | idxFrame = pSegLines->idxGrp;
|
---|
4467 | bFrame = OMF_FIX_F_GRPDEF;
|
---|
4468 | }
|
---|
4469 |
|
---|
4470 | /* Fixup #1: segment offset - IMAGE_REL_AMD64_SECREL. */
|
---|
4471 | if (!omfWriter_LEDataAddFixupNoDisp(pThis, 4 + 4 + RT_OFFSETOF(RTCV8LINESHDR, offSection), OMF_FIX_LOC_32BIT_OFFSET,
|
---|
4472 | bFrame, idxFrame, OMF_FIX_T_SEGDEF_NO_DISP, pSegLines->idxSeg))
|
---|
4473 | return false;
|
---|
4474 |
|
---|
4475 |
|
---|
4476 | /* Fixup #2: segment number - IMAGE_REL_AMD64_SECTION. */
|
---|
4477 | if (!omfWriter_LEDataAddFixupNoDisp(pThis, 4 + 4 + RT_OFFSETOF(RTCV8LINESHDR, iSection), OMF_FIX_LOC_16BIT_SEGMENT,
|
---|
4478 | bFrame, idxFrame, OMF_FIX_T_SEGDEF_NO_DISP, pSegLines->idxSeg))
|
---|
4479 | return false;
|
---|
4480 |
|
---|
4481 | /* Emit data for each source file. */
|
---|
4482 | for (j = 0; j < pSegLines->cFiles; j++)
|
---|
4483 | {
|
---|
4484 | uint32_t const cbPairs = pSegLines->paFiles[j].cPairs * sizeof(RTCV8LINEPAIR);
|
---|
4485 | if ( !omfWriter_LEDataAddU32(pThis, pSegLines->paFiles[j].offSrcInfo) /*RTCV8LINESSRCMAP::offSourceInfo*/
|
---|
4486 | || !omfWriter_LEDataAddU32(pThis, pSegLines->paFiles[j].cPairs) /*RTCV8LINESSRCMAP::cLines*/
|
---|
4487 | || !omfWriter_LEDataAddU32(pThis, cbPairs + sizeof(RTCV8LINESSRCMAP)) /*RTCV8LINESSRCMAP::cb*/
|
---|
4488 | || !omfWriter_LEDataAddBytes(pThis, pSegLines->paFiles[j].paPairs, cbPairs))
|
---|
4489 | return false;
|
---|
4490 | }
|
---|
4491 | }
|
---|
4492 | pSegLines++;
|
---|
4493 | }
|
---|
4494 |
|
---|
4495 | return omfWriter_LEDataEnd(pThis);
|
---|
4496 | }
|
---|
4497 |
|
---|
4498 |
|
---|
4499 | /**
|
---|
4500 | * This does the actual converting, passthru style.
|
---|
4501 | *
|
---|
4502 | * It only modifies, removes and inserts stuff it care about, the rest is passed
|
---|
4503 | * thru as-is.
|
---|
4504 | *
|
---|
4505 | * @returns success indicator.
|
---|
4506 | * @param pThis The OMF writer.
|
---|
4507 | * @param pbFile The original file content.
|
---|
4508 | * @param cbFile The size of the original file.
|
---|
4509 | * @param pOmfStuff The OMF stuff we've gathered during the first pass,
|
---|
4510 | * contains CV8 line number info if we converted anything.
|
---|
4511 | */
|
---|
4512 | static bool convertOmfPassthru(POMFWRITER pThis, uint8_t const *pbFile, size_t cbFile, POMFDETAILS pOmfStuff)
|
---|
4513 | {
|
---|
4514 | bool const fConvertLineNumbers = true;
|
---|
4515 | bool fSeenTheAdr = false;
|
---|
4516 | uint32_t off = 0;
|
---|
4517 | while (off + 3 < cbFile)
|
---|
4518 | {
|
---|
4519 | uint8_t bRecType = pbFile[off];
|
---|
4520 | uint16_t cbRec = RT_MAKE_U16(pbFile[off + 1], pbFile[off + 2]);
|
---|
4521 | uint32_t offRec = 0;
|
---|
4522 | uint8_t const *pbRec = &pbFile[off + 3];
|
---|
4523 |
|
---|
4524 | #define OMF_READ_IDX(a_idx, a_Name) \
|
---|
4525 | do { \
|
---|
4526 | a_idx = pbRec[offRec++]; \
|
---|
4527 | if ((a_idx) & 0x80) \
|
---|
4528 | a_idx = (((a_idx) & 0x7f) << 8) | pbRec[offRec++]; \
|
---|
4529 | } while (0)
|
---|
4530 |
|
---|
4531 | /*
|
---|
4532 | * Remove/insert switch. will
|
---|
4533 | */
|
---|
4534 | bool fSkip = false;
|
---|
4535 | switch (bRecType)
|
---|
4536 | {
|
---|
4537 | /*
|
---|
4538 | * Mangle watcom intrinsics if necessary.
|
---|
4539 | */
|
---|
4540 | case OMF_EXTDEF:
|
---|
4541 | if (pOmfStuff->fMayNeedMangling)
|
---|
4542 | {
|
---|
4543 | if (!omfWriter_ExtDefBegin(pThis))
|
---|
4544 | return false;
|
---|
4545 | while (offRec + 1 < cbRec)
|
---|
4546 | {
|
---|
4547 | uint8_t cchName = pbRec[offRec++];
|
---|
4548 | char *pchName = (char *)&pbRec[offRec];
|
---|
4549 | offRec += cchName;
|
---|
4550 |
|
---|
4551 | uint16_t idxType;
|
---|
4552 | OMF_READ_IDX(idxType, EXTDEF);
|
---|
4553 |
|
---|
4554 | /* Look for g_apszExtDefRenames entries that requires changing. */
|
---|
4555 | if ( cchName >= 5
|
---|
4556 | && cchName <= 7
|
---|
4557 | && pchName[0] == '_'
|
---|
4558 | && pchName[1] == '_'
|
---|
4559 | && ( pchName[2] == 'U'
|
---|
4560 | || pchName[2] == 'I'
|
---|
4561 | || pchName[2] == 'P')
|
---|
4562 | && ( pchName[3] == '4'
|
---|
4563 | || pchName[3] == '8'
|
---|
4564 | || pchName[3] == 'I'
|
---|
4565 | || pchName[3] == 'T') )
|
---|
4566 | {
|
---|
4567 | char szName[12];
|
---|
4568 | memcpy(szName, pchName, cchName);
|
---|
4569 | szName[cchName] = '\0';
|
---|
4570 |
|
---|
4571 | uint32_t i = RT_ELEMENTS(g_apszExtDefRenames);
|
---|
4572 | while (i-- > 0)
|
---|
4573 | if ( cchName == (uint8_t)g_apszExtDefRenames[i][0]
|
---|
4574 | && memcmp(&g_apszExtDefRenames[i][1], szName, cchName) == 0)
|
---|
4575 | {
|
---|
4576 | szName[0] = pOmfStuff->fProbably32bit ? '?' : '_';
|
---|
4577 | szName[1] = '?';
|
---|
4578 | break;
|
---|
4579 | }
|
---|
4580 |
|
---|
4581 | if (!omfWriter_ExtDefAddN(pThis, szName, cchName, idxType))
|
---|
4582 | return false;
|
---|
4583 | }
|
---|
4584 | else if (!omfWriter_ExtDefAddN(pThis, pchName, cchName, idxType))
|
---|
4585 | return false;
|
---|
4586 | }
|
---|
4587 | if (!omfWriter_ExtDefEnd(pThis))
|
---|
4588 | return false;
|
---|
4589 | fSkip = true;
|
---|
4590 | }
|
---|
4591 | break;
|
---|
4592 |
|
---|
4593 | /*
|
---|
4594 | * Remove line number records.
|
---|
4595 | */
|
---|
4596 | case OMF_LINNUM16:
|
---|
4597 | case OMF_LINNUM32:
|
---|
4598 | fSkip = fConvertLineNumbers;
|
---|
4599 | break;
|
---|
4600 |
|
---|
4601 | /*
|
---|
4602 | * Remove all but the first OMF_THEADR.
|
---|
4603 | */
|
---|
4604 | case OMF_THEADR:
|
---|
4605 | fSkip = fSeenTheAdr && fConvertLineNumbers;
|
---|
4606 | fSeenTheAdr = true;
|
---|
4607 | break;
|
---|
4608 |
|
---|
4609 | /*
|
---|
4610 | * Remove borland source file changes. Also, emit our SEGDEF
|
---|
4611 | * before the pass marker.
|
---|
4612 | */
|
---|
4613 | case OMF_COMENT:
|
---|
4614 | if (fConvertLineNumbers)
|
---|
4615 | {
|
---|
4616 | fSkip = pbRec[1] == OMF_CCLS_BORLAND_SRC_FILE;
|
---|
4617 | if (pbRec[1] == OMF_CCLS_LINK_PASS_SEP)
|
---|
4618 | if ( !convertOmfWriteDebugSegDefs(pThis, pOmfStuff)
|
---|
4619 | || !convertOmfWriteDebugGrpDefs(pThis, pOmfStuff))
|
---|
4620 | return false;
|
---|
4621 | }
|
---|
4622 | break;
|
---|
4623 |
|
---|
4624 | /*
|
---|
4625 | * Redo these to the OMF writer is on top of the index thing.
|
---|
4626 | */
|
---|
4627 | case OMF_LNAMES:
|
---|
4628 | if (!omfWriter_LNamesBegin(pThis, false /*fAddZeroEntry*/))
|
---|
4629 | return false;
|
---|
4630 | while (offRec + 1 < cbRec)
|
---|
4631 | {
|
---|
4632 | uint8_t cch = pbRec[offRec];
|
---|
4633 | const char *pch = (const char *)&pbRec[offRec + 1];
|
---|
4634 | if (!omfWriter_LNamesAddN(pThis, pch, cch, NULL))
|
---|
4635 | return false;
|
---|
4636 | offRec += cch + 1;
|
---|
4637 | }
|
---|
4638 | if (!omfWriter_LNamesEnd(pThis))
|
---|
4639 | return false;
|
---|
4640 |
|
---|
4641 | fSkip = true;
|
---|
4642 | break;
|
---|
4643 |
|
---|
4644 | /*
|
---|
4645 | * Upon seeing MODEND we write out the debug info.
|
---|
4646 | */
|
---|
4647 | case OMF_MODEND16:
|
---|
4648 | case OMF_MODEND32:
|
---|
4649 | if (fConvertLineNumbers)
|
---|
4650 | {
|
---|
4651 | if ( convertOmfWriteDebugSegDefs(pThis, pOmfStuff)
|
---|
4652 | && convertOmfWriteDebugGrpDefs(pThis, pOmfStuff)
|
---|
4653 | && convertOmfWriteDebugData(pThis, pOmfStuff))
|
---|
4654 | { /* likely */ }
|
---|
4655 | else return false;
|
---|
4656 | }
|
---|
4657 | break;
|
---|
4658 | }
|
---|
4659 |
|
---|
4660 | /*
|
---|
4661 | * Pass the record thru, if so was decided.
|
---|
4662 | */
|
---|
4663 | if (!fSkip)
|
---|
4664 | {
|
---|
4665 | if ( omfWriter_RecBegin(pThis, bRecType)
|
---|
4666 | && omfWriter_RecAddBytes(pThis, pbRec, cbRec)
|
---|
4667 | && omfWriter_RecEnd(pThis, false))
|
---|
4668 | { /* likely */ }
|
---|
4669 | else return false;
|
---|
4670 | }
|
---|
4671 |
|
---|
4672 | /* advance */
|
---|
4673 | off += cbRec + 3;
|
---|
4674 | }
|
---|
4675 |
|
---|
4676 | return true;
|
---|
4677 | }
|
---|
4678 |
|
---|
4679 |
|
---|
4680 | /**
|
---|
4681 | * Converts LINNUMs and compiler intrinsics in an OMF object file.
|
---|
4682 | *
|
---|
4683 | * Wlink does a cheesy (to use their own term) job of generating the
|
---|
4684 | * sstSrcModule subsection. It is limited to one file and cannot deal with line
|
---|
4685 | * numbers in different segment. The latter is very annoying in assembly files
|
---|
4686 | * that jumps between segments, these a frequent on crash stacks.
|
---|
4687 | *
|
---|
4688 | * The solution is to convert to the same line number tables that cl.exe /Z7
|
---|
4689 | * generates for our 64-bit C code, we named that format codeview v8, or CV8.
|
---|
4690 | * Our code codeview debug info reader can deal with this already because of the
|
---|
4691 | * 64-bit code, so Bob's your uncle.
|
---|
4692 | *
|
---|
4693 | * @returns success indicator.
|
---|
4694 | * @param pszFile The name of the file being converted.
|
---|
4695 | * @param pbFile The file content.
|
---|
4696 | * @param cbFile The size of the file content.
|
---|
4697 | * @param pDst The destiation (output) file.
|
---|
4698 | */
|
---|
4699 | static bool convertOmfToOmf(const char *pszFile, uint8_t const *pbFile, size_t cbFile, FILE *pDst)
|
---|
4700 | {
|
---|
4701 | /*
|
---|
4702 | * Collect line number information.
|
---|
4703 | */
|
---|
4704 | OMFDETAILS OmfStuff;
|
---|
4705 | if (!collectOmfDetails(pszFile, pbFile, cbFile, &OmfStuff))
|
---|
4706 | return false;
|
---|
4707 |
|
---|
4708 | /*
|
---|
4709 | * Instantiate the OMF writer and do pass-thru modifications.
|
---|
4710 | */
|
---|
4711 | bool fRc;
|
---|
4712 | POMFWRITER pThis = omfWriter_Create(pszFile, 0, 0, pDst);
|
---|
4713 | if (pThis)
|
---|
4714 | {
|
---|
4715 | fRc = convertOmfPassthru(pThis, pbFile, cbFile, &OmfStuff);
|
---|
4716 | omfWriter_Destroy(pThis);
|
---|
4717 | }
|
---|
4718 | else
|
---|
4719 | fRc = false;
|
---|
4720 |
|
---|
4721 |
|
---|
4722 | /*
|
---|
4723 | * Cleanup OmfStuff.
|
---|
4724 | */
|
---|
4725 | uint32_t i = OmfStuff.cSegLines;
|
---|
4726 | while (i-- >0)
|
---|
4727 | {
|
---|
4728 | uint32_t j = OmfStuff.paSegLines[i].cFiles;
|
---|
4729 | while (j-- > 0)
|
---|
4730 | free(OmfStuff.paSegLines[i].paFiles[j].paPairs);
|
---|
4731 | free(OmfStuff.paSegLines[i].paFiles);
|
---|
4732 | }
|
---|
4733 | free(OmfStuff.paSegLines);
|
---|
4734 | free(OmfStuff.paSrcInfo);
|
---|
4735 | free(OmfStuff.pchStrTab);
|
---|
4736 | return fRc;
|
---|
4737 | }
|
---|
4738 |
|
---|
4739 |
|
---|
4740 | /**
|
---|
4741 | * Does the convertion using convertelf and convertcoff.
|
---|
4742 | *
|
---|
4743 | * @returns exit code (0 on success, non-zero on failure)
|
---|
4744 | * @param pszFile The file to convert.
|
---|
4745 | */
|
---|
4746 | static int convertit(const char *pszFile)
|
---|
4747 | {
|
---|
4748 | /* Construct the filename for saving the unmodified file. */
|
---|
4749 | char szOrgFile[_4K];
|
---|
4750 | size_t cchFile = strlen(pszFile);
|
---|
4751 | if (cchFile + sizeof(".original") > sizeof(szOrgFile))
|
---|
4752 | {
|
---|
4753 | error(pszFile, "Filename too long!\n");
|
---|
4754 | return RTEXITCODE_FAILURE;
|
---|
4755 | }
|
---|
4756 | memcpy(szOrgFile, pszFile, cchFile);
|
---|
4757 | memcpy(&szOrgFile[cchFile], ".original", sizeof(".original"));
|
---|
4758 |
|
---|
4759 | /* Read the whole file. */
|
---|
4760 | void *pvFile;
|
---|
4761 | size_t cbFile;
|
---|
4762 | if (readfile(pszFile, &pvFile, &cbFile))
|
---|
4763 | {
|
---|
4764 | /*
|
---|
4765 | * Do format conversions / adjustments.
|
---|
4766 | */
|
---|
4767 | bool fRc = false;
|
---|
4768 | uint8_t *pbFile = (uint8_t *)pvFile;
|
---|
4769 | if ( cbFile > sizeof(Elf64_Ehdr)
|
---|
4770 | && pbFile[0] == ELFMAG0
|
---|
4771 | && pbFile[1] == ELFMAG1
|
---|
4772 | && pbFile[2] == ELFMAG2
|
---|
4773 | && pbFile[3] == ELFMAG3)
|
---|
4774 | {
|
---|
4775 | if (writefile(szOrgFile, pvFile, cbFile))
|
---|
4776 | {
|
---|
4777 | FILE *pDst = openfile(pszFile, true /*fWrite*/);
|
---|
4778 | if (pDst)
|
---|
4779 | {
|
---|
4780 | fRc = convertElfToOmf(pszFile, pbFile, cbFile, pDst);
|
---|
4781 | fRc = fclose(pDst) == 0 && fRc;
|
---|
4782 | }
|
---|
4783 | }
|
---|
4784 | }
|
---|
4785 | else if ( cbFile > sizeof(IMAGE_FILE_HEADER)
|
---|
4786 | && RT_MAKE_U16(pbFile[0], pbFile[1]) == IMAGE_FILE_MACHINE_AMD64
|
---|
4787 | && RT_MAKE_U16(pbFile[2], pbFile[3]) * sizeof(IMAGE_SECTION_HEADER) + sizeof(IMAGE_FILE_HEADER)
|
---|
4788 | < cbFile
|
---|
4789 | && RT_MAKE_U16(pbFile[2], pbFile[3]) > 0)
|
---|
4790 | {
|
---|
4791 | if (writefile(szOrgFile, pvFile, cbFile))
|
---|
4792 | {
|
---|
4793 | FILE *pDst = openfile(pszFile, true /*fWrite*/);
|
---|
4794 | if (pDst)
|
---|
4795 | {
|
---|
4796 | fRc = convertCoffToOmf(pszFile, pbFile, cbFile, pDst);
|
---|
4797 | fRc = fclose(pDst) == 0 && fRc;
|
---|
4798 | }
|
---|
4799 | }
|
---|
4800 | }
|
---|
4801 | else if ( cbFile >= 8
|
---|
4802 | && pbFile[0] == OMF_THEADR
|
---|
4803 | && RT_MAKE_U16(pbFile[1], pbFile[2]) < cbFile)
|
---|
4804 | {
|
---|
4805 | if (writefile(szOrgFile, pvFile, cbFile))
|
---|
4806 | {
|
---|
4807 | FILE *pDst = openfile(pszFile, true /*fWrite*/);
|
---|
4808 | if (pDst)
|
---|
4809 | {
|
---|
4810 | fRc = convertOmfToOmf(pszFile, pbFile, cbFile, pDst);
|
---|
4811 | fRc = fclose(pDst) == 0 && fRc;
|
---|
4812 | }
|
---|
4813 | }
|
---|
4814 | }
|
---|
4815 | else
|
---|
4816 | fprintf(stderr, "error: Don't recognize format of '%s' (%#x %#x %#x %#x, cbFile=%lu)\n",
|
---|
4817 | pszFile, pbFile[0], pbFile[1], pbFile[2], pbFile[3], (unsigned long)cbFile);
|
---|
4818 | free(pvFile);
|
---|
4819 | if (fRc)
|
---|
4820 | return 0;
|
---|
4821 | }
|
---|
4822 | return 1;
|
---|
4823 | }
|
---|
4824 |
|
---|
4825 |
|
---|
4826 | int main(int argc, char **argv)
|
---|
4827 | {
|
---|
4828 | int rcExit = 0;
|
---|
4829 |
|
---|
4830 | /*
|
---|
4831 | * Scan the arguments.
|
---|
4832 | */
|
---|
4833 | for (int i = 1; i < argc; i++)
|
---|
4834 | {
|
---|
4835 | if (argv[i][0] == '-')
|
---|
4836 | {
|
---|
4837 | const char *pszOpt = &argv[i][1];
|
---|
4838 | if (*pszOpt == '-')
|
---|
4839 | {
|
---|
4840 | /* Convert long options to short ones. */
|
---|
4841 | pszOpt--;
|
---|
4842 | if (!strcmp(pszOpt, "--verbose"))
|
---|
4843 | pszOpt = "v";
|
---|
4844 | else if (!strcmp(pszOpt, "--version"))
|
---|
4845 | pszOpt = "V";
|
---|
4846 | else if (!strcmp(pszOpt, "--help"))
|
---|
4847 | pszOpt = "h";
|
---|
4848 | else
|
---|
4849 | {
|
---|
4850 | fprintf(stderr, "syntax errro: Unknown options '%s'\n", pszOpt);
|
---|
4851 | return 2;
|
---|
4852 | }
|
---|
4853 | }
|
---|
4854 |
|
---|
4855 | /* Process the list of short options. */
|
---|
4856 | while (*pszOpt)
|
---|
4857 | {
|
---|
4858 | switch (*pszOpt++)
|
---|
4859 | {
|
---|
4860 | case 'v':
|
---|
4861 | g_cVerbose++;
|
---|
4862 | break;
|
---|
4863 |
|
---|
4864 | case 'V':
|
---|
4865 | printf("%s\n", "$Revision: 60527 $");
|
---|
4866 | return 0;
|
---|
4867 |
|
---|
4868 | case '?':
|
---|
4869 | case 'h':
|
---|
4870 | printf("usage: %s [options] -o <output> <input1> [input2 ... [inputN]]\n",
|
---|
4871 | argv[0]);
|
---|
4872 | return 0;
|
---|
4873 | }
|
---|
4874 | }
|
---|
4875 | }
|
---|
4876 | else
|
---|
4877 | {
|
---|
4878 | /*
|
---|
4879 | * File to convert. Do the job right away.
|
---|
4880 | */
|
---|
4881 | rcExit = convertit(argv[i]);
|
---|
4882 | if (rcExit != 0)
|
---|
4883 | break;
|
---|
4884 | }
|
---|
4885 | }
|
---|
4886 |
|
---|
4887 | return rcExit;
|
---|
4888 | }
|
---|
4889 |
|
---|
4890 |
|
---|