1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2014 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * rdlib.c - routines for manipulating RDOFF libraries (.rdl)
47 /* See Texinfo documentation about new RDOFF libraries format */
51 char *rdl_errors
[5] = {
52 "no error", "could not open file", "invalid file structure",
53 "file contains modules of an unsupported RDOFF version",
57 int rdl_verify(const char *filename
)
63 static char lastverified
[256];
64 static int lastresult
= -1;
66 if (lastresult
!= -1 && !strcmp(filename
, lastverified
))
69 fp
= fopen(filename
, "rb");
70 strcpy(lastverified
, filename
);
73 return (rdl_error
= lastresult
= 1);
78 while (fread(buf
+ i
, 1, 1, fp
) == 1 && i
< 257 && buf
[i
])
85 * A special module, eg a signature block or a directory.
86 * Format of such a module is defined to be:
87 * six char type identifier
88 * int32_t count bytes content
90 * so we can handle it uniformaly with RDOFF2 modules.
92 nasm_read(buf
, 6, fp
);
94 /* Currently, nothing useful to do with signature block.. */
96 nasm_read(buf
, 6, fp
);
98 if (strncmp(buf
, "RDOFF", 5)) {
100 return rdl_error
= lastresult
= 2;
101 } else if (buf
[5] != '2') {
103 return rdl_error
= lastresult
= 3;
106 nasm_read(&length
, 4, fp
);
107 fseek(fp
, length
, SEEK_CUR
); /* skip over the module */
110 return lastresult
= 0; /* library in correct format */
113 int rdl_open(struct librarynode
*lib
, const char *name
)
115 int i
= rdl_verify(name
);
120 lib
->name
= nasm_strdup(name
);
126 int rdl_searchlib(struct librarynode
*lib
, const char *label
, rdffile
* f
)
138 lib
->fp
= fopen(lib
->name
, "rb");
147 while (!feof(lib
->fp
)) {
149 * read the module name from the file, and prepend
150 * the library name and '.' to it.
152 strcpy(buf
, lib
->name
);
154 i
= strlen(lib
->name
);
157 while (fread(buf
+ i
, 1, 1, lib
->fp
) == 1 && i
< 512 && buf
[i
])
164 if (!strcmp(buf
+ t
, ".dir")) { /* skip over directory */
165 nasm_read(&l
, 4, lib
->fp
);
166 fseek(lib
->fp
, l
, SEEK_CUR
);
170 * open the RDOFF module
172 if (rdfopenhere(f
, lib
->fp
, &lib
->referenced
, buf
)) {
173 rdl_error
= 16 * rdf_errno
;
177 * read in the header, and scan for exported symbols
179 hdr
= nasm_malloc(f
->header_len
);
180 rdfloadseg(f
, RDOFF_HEADER
, hdr
);
182 while ((r
= rdfgetheaderrec(f
))) {
183 if (r
->type
!= 3) /* not an export */
186 if (!strcmp(r
->e
.label
, label
)) { /* match! */
187 nasm_free(hdr
); /* reset to 'just open' */
188 f
->header_loc
= NULL
; /* state... */
194 /* find start of next module... */
197 fseek(lib
->fp
, i
, SEEK_SET
);
201 * close the file if nobody else is using it
204 if (!lib
->referenced
) {
211 int rdl_openmodule(struct librarynode
*lib
, int moduleno
, rdffile
* f
)
220 lib
->fp
= fopen(lib
->name
, "rb");
223 return (rdl_error
= 1);
229 while (!feof(lib
->fp
)) {
230 strcpy(buf
, lib
->name
);
234 while (fread(buf
+ i
, 1, 1, lib
->fp
) == 1 && i
< 512 && buf
[i
])
240 if (buf
[t
] != '.') /* special module - not counted in the numbering */
241 cmod
++; /* of RDOFF modules - must be referred to by name */
243 if (cmod
== moduleno
) {
245 rdfopenhere(f
, lib
->fp
, &lib
->referenced
, buf
);
247 if (!lib
->referenced
) {
254 nasm_read(buf
, 6, lib
->fp
);
258 } else if (strncmp(buf
, "RDOFF", 5)) {
259 if (!--lib
->referenced
) {
263 return rdl_error
= 2;
264 } else if (buf
[5] != '2') {
265 if (!--lib
->referenced
) {
269 return rdl_error
= 3;
272 nasm_read(&length
, 4, lib
->fp
);
273 fseek(lib
->fp
, length
, SEEK_CUR
); /* skip over the module */
275 if (!--lib
->referenced
) {
279 return rdl_error
= 4; /* module not found */
282 void rdl_perror(const char *apname
, const char *filename
)
285 rdfperror(apname
, filename
);
287 fprintf(stderr
, "%s:%s:%s\n", apname
, filename
,
288 rdl_errors
[rdl_error
]);