1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 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)
50 /* See Texinfo documentation about new RDOFF libraries format */
54 char *rdl_errors
[5] = {
55 "no error", "could not open file", "invalid file structure",
56 "file contains modules of an unsupported RDOFF version",
60 int rdl_verify(const char *filename
)
66 static char lastverified
[256];
67 static int lastresult
= -1;
69 if (lastresult
!= -1 && !strcmp(filename
, lastverified
))
72 fp
= fopen(filename
, "rb");
73 strcpy(lastverified
, filename
);
76 return (rdl_error
= lastresult
= 1);
81 while (fread(buf
+ i
, 1, 1, fp
) == 1 && i
< 257 && buf
[i
])
88 * A special module, eg a signature block or a directory.
89 * Format of such a module is defined to be:
90 * six char type identifier
91 * int32_t count bytes content
93 * so we can handle it uniformaly with RDOFF2 modules.
97 /* Currently, nothing useful to do with signature block.. */
101 if (strncmp(buf
, "RDOFF", 5)) {
103 return rdl_error
= lastresult
= 2;
104 } else if (buf
[5] != '2') {
106 return rdl_error
= lastresult
= 3;
109 fread(&length
, 4, 1, fp
);
110 fseek(fp
, length
, SEEK_CUR
); /* skip over the module */
113 return lastresult
= 0; /* library in correct format */
116 int rdl_open(struct librarynode
*lib
, const char *name
)
118 int i
= rdl_verify(name
);
123 lib
->name
= strdup(name
);
129 void rdl_close(struct librarynode
*lib
)
136 int rdl_searchlib(struct librarynode
*lib
, const char *label
, rdffile
* f
)
148 lib
->fp
= fopen(lib
->name
, "rb");
157 while (!feof(lib
->fp
)) {
159 * read the module name from the file, and prepend
160 * the library name and '.' to it.
162 strcpy(buf
, lib
->name
);
164 i
= strlen(lib
->name
);
167 while (fread(buf
+ i
, 1, 1, lib
->fp
) == 1 && i
< 512 && buf
[i
])
174 if (!strcmp(buf
+ t
, ".dir")) { /* skip over directory */
175 fread(&l
, 4, 1, lib
->fp
);
176 fseek(lib
->fp
, l
, SEEK_CUR
);
180 * open the RDOFF module
182 if (rdfopenhere(f
, lib
->fp
, &lib
->referenced
, buf
)) {
183 rdl_error
= 16 * rdf_errno
;
187 * read in the header, and scan for exported symbols
189 hdr
= malloc(f
->header_len
);
190 rdfloadseg(f
, RDOFF_HEADER
, hdr
);
192 while ((r
= rdfgetheaderrec(f
))) {
193 if (r
->type
!= 3) /* not an export */
196 if (!strcmp(r
->e
.label
, label
)) { /* match! */
197 free(hdr
); /* reset to 'just open' */
198 f
->header_loc
= NULL
; /* state... */
204 /* find start of next module... */
207 fseek(lib
->fp
, i
, SEEK_SET
);
211 * close the file if nobody else is using it
214 if (!lib
->referenced
) {
221 int rdl_openmodule(struct librarynode
*lib
, int moduleno
, rdffile
* f
)
230 lib
->fp
= fopen(lib
->name
, "rb");
233 return (rdl_error
= 1);
239 while (!feof(lib
->fp
)) {
240 strcpy(buf
, lib
->name
);
244 while (fread(buf
+ i
, 1, 1, lib
->fp
) == 1 && i
< 512 && buf
[i
])
250 if (buf
[t
] != '.') /* special module - not counted in the numbering */
251 cmod
++; /* of RDOFF modules - must be referred to by name */
253 if (cmod
== moduleno
) {
255 rdfopenhere(f
, lib
->fp
, &lib
->referenced
, buf
);
257 if (!lib
->referenced
) {
264 fread(buf
, 6, 1, lib
->fp
);
268 } else if (strncmp(buf
, "RDOFF", 5)) {
269 if (!--lib
->referenced
) {
273 return rdl_error
= 2;
274 } else if (buf
[5] != '2') {
275 if (!--lib
->referenced
) {
279 return rdl_error
= 3;
282 fread(&length
, 4, 1, lib
->fp
);
283 fseek(lib
->fp
, length
, SEEK_CUR
); /* skip over the module */
285 if (!--lib
->referenced
) {
289 return rdl_error
= 4; /* module not found */
292 void rdl_perror(const char *apname
, const char *filename
)
295 rdfperror(apname
, filename
);
297 fprintf(stderr
, "%s:%s:%s\n", apname
, filename
,
298 rdl_errors
[rdl_error
]);