1 | /** $Id: dotseg.cpp 75337 2018-11-09 01:39:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxSF - OS/2 Shared Folders, NASM Object File Editor for DWARF segments.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2018 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 | /*********************************************************************************************************************************
|
---|
34 | * Header Files *
|
---|
35 | *********************************************************************************************************************************/
|
---|
36 | #include <iprt/types.h>
|
---|
37 | #include <iprt/formats/omf.h>
|
---|
38 |
|
---|
39 | #include <stdio.h>
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 | int main(int argc, char **argv)
|
---|
44 | {
|
---|
45 | if (argc != 2)
|
---|
46 | {
|
---|
47 | fprintf(stderr, "syntax error! Expected exactly one argument, found %d!\n", argc - 1);
|
---|
48 | return 2;
|
---|
49 | }
|
---|
50 | const char *pszFilename = argv[1];
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * Open the file.
|
---|
54 | */
|
---|
55 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
56 | FILE *pFile = fopen(pszFilename, "r+b");
|
---|
57 | #else
|
---|
58 | FILE *pFile = fopen(pszFilename, "r+b");
|
---|
59 | #endif
|
---|
60 | if (!pFile)
|
---|
61 | {
|
---|
62 | fprintf(stderr, "error opening '%s' for updating!\n", pszFilename);
|
---|
63 | return 1;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * Parse the file.
|
---|
68 | */
|
---|
69 | uint32_t offRec = 0;
|
---|
70 | while (!feof(pFile))
|
---|
71 | {
|
---|
72 | OMFRECHDR Hdr;
|
---|
73 | if (fread(&Hdr, sizeof(Hdr), 1, pFile) != 1)
|
---|
74 | break;
|
---|
75 | /*fprintf(stderr, "dbg: %#07x: %02x %04x\n", offRec, Hdr.bType, Hdr.cbLen);*/
|
---|
76 | uint8_t abData[OMF_MAX_RECORD_LENGTH];
|
---|
77 | if (Hdr.cbLen > sizeof(abData))
|
---|
78 | {
|
---|
79 | fprintf(stderr, "%#07x: bad record: cbLen=%#x\n", offRec, Hdr.cbLen);
|
---|
80 | return 1;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /* Is it interesting? */
|
---|
84 | if (Hdr.bType == OMF_LNAMES)
|
---|
85 | {
|
---|
86 | /* Read the whole record. */
|
---|
87 | if (fread(abData, Hdr.cbLen, 1, pFile) != 1)
|
---|
88 | break;
|
---|
89 |
|
---|
90 | /* Scan it and make updates. */
|
---|
91 | bool fUpdated = false;
|
---|
92 | for (unsigned offData = 0; offData + 1 < Hdr.cbLen; )
|
---|
93 | {
|
---|
94 | uint8_t cchName = abData[offData++];
|
---|
95 | if (offData + cchName + 1 > Hdr.cbLen)
|
---|
96 | {
|
---|
97 | fprintf(stderr, "%#07x: bad LNAMES record (offData=3 + %#x)\n", offRec, offData);
|
---|
98 | return 1;
|
---|
99 | }
|
---|
100 | if ( cchName > 5
|
---|
101 | && abData[offData + 0] == '_'
|
---|
102 | && abData[offData + 1] == 'd'
|
---|
103 | && abData[offData + 2] == 'e'
|
---|
104 | && abData[offData + 3] == 'b'
|
---|
105 | && abData[offData + 4] == 'u'
|
---|
106 | && abData[offData + 5] == 'g')
|
---|
107 | {
|
---|
108 | abData[offData] = '.';
|
---|
109 | fUpdated = true;
|
---|
110 | }
|
---|
111 | offData += cchName;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* Write out updates. */
|
---|
115 | if (fUpdated)
|
---|
116 | {
|
---|
117 | abData[Hdr.cbLen - 1] = 0; /* squash crc */
|
---|
118 | if ( fseek(pFile, offRec + 3, SEEK_SET) != 0
|
---|
119 | || fwrite(abData, Hdr.cbLen, 1, pFile) != 1
|
---|
120 | || fseek(pFile, offRec + 3 + Hdr.cbLen, SEEK_SET) != 0)
|
---|
121 | {
|
---|
122 | fprintf(stderr, "%#07x: error writing %#x bytes\n", offRec, Hdr.cbLen);
|
---|
123 | return 1;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | /* Not interesting, so skip it and the CRC. */
|
---|
128 | else if (fseek(pFile, Hdr.cbLen, SEEK_CUR) != 0)
|
---|
129 | {
|
---|
130 | fprintf(stderr, "%#07x: error skipping %#x bytes\n", offRec, Hdr.cbLen);
|
---|
131 | return 1;
|
---|
132 | }
|
---|
133 | offRec += 3 + Hdr.cbLen;
|
---|
134 | }
|
---|
135 |
|
---|
136 | if (ferror(pFile))
|
---|
137 | {
|
---|
138 | fprintf(stderr, "read error\n");
|
---|
139 | return 1;
|
---|
140 | }
|
---|
141 | if (fclose(pFile) != 0)
|
---|
142 | {
|
---|
143 | fprintf(stderr, "error flush/closing file\n");
|
---|
144 | return 1;
|
---|
145 | }
|
---|
146 | return 0;
|
---|
147 | }
|
---|
148 |
|
---|