comparison lwlink/lwlink.c @ 0:2c24602be78f

Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
author lost@l-w.ca
date Wed, 19 Jan 2011 22:27:17 -0700
parents
children fdc11ef4115b
comparison
equal deleted inserted replaced
-1:000000000000 0:2c24602be78f
1 /*
2 lwlink.c
3 Copyright © 2009 William Astle
4
5 This file is part of LWLINK.
6
7 LWLINK is free software: you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation, either version 3 of the License, or (at your option) any later
10 version.
11
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 more details.
16
17 You should have received a copy of the GNU General Public License along with
18 this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21
22 */
23
24 #define __lwlink_c_seen__
25
26 #include <argp.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "lwlink.h"
33 #include "util.h"
34
35 int debug_level = 0;
36 int outformat = OUTPUT_DECB;
37 char *outfile = NULL;
38 char *scriptfile = NULL;
39 int symerr = 0;
40 char *map_file = NULL;
41
42 fileinfo_t **inputfiles = NULL;
43 int ninputfiles = 0;
44
45 int nlibdirs = 0;
46 char **libdirs = NULL;
47
48 int nscriptls = 0;
49 char **scriptls = NULL;
50
51 void add_input_file(char *fn)
52 {
53 inputfiles = lw_realloc(inputfiles, sizeof(fileinfo_t *) * (ninputfiles + 1));
54 inputfiles[ninputfiles] = lw_malloc(sizeof(fileinfo_t));
55 memset(inputfiles[ninputfiles], 0, sizeof(fileinfo_t));
56 inputfiles[ninputfiles] -> forced = 1;
57 inputfiles[ninputfiles++] -> filename = lw_strdup(fn);
58 }
59
60 void add_input_library(char *libname)
61 {
62 inputfiles = lw_realloc(inputfiles, sizeof(fileinfo_t *) * (ninputfiles + 1));
63 inputfiles[ninputfiles] = lw_malloc(sizeof(fileinfo_t));
64 memset(inputfiles[ninputfiles], 0, sizeof(fileinfo_t));
65 inputfiles[ninputfiles] -> islib = 1;
66 inputfiles[ninputfiles] -> forced = 0;
67 inputfiles[ninputfiles++] -> filename = lw_strdup(libname);
68 }
69
70 void add_library_search(char *libdir)
71 {
72 libdirs = lw_realloc(libdirs, sizeof(char*) * (nlibdirs + 1));
73 libdirs[nlibdirs] = lw_strdup(libdir);
74 nlibdirs++;
75 }
76
77 void add_section_base(char *sectspec)
78 {
79 char *base;
80 int baseaddr;
81 char *t;
82 int l;
83
84 base = strchr(sectspec, '=');
85 if (!base)
86 {
87 l = strlen(sectspec);
88 baseaddr = 0;
89 }
90 else
91 {
92 baseaddr = strtol(base + 1, NULL, 16);
93 l = base - sectspec;
94 *base = '\0';
95 }
96 baseaddr = baseaddr & 0xffff;
97
98 t = lw_malloc(l + 25);
99 sprintf(t, "section %s load %04X", sectspec, baseaddr);
100 if (base)
101 *base = '=';
102
103 scriptls = lw_realloc(scriptls, sizeof(char *) * (nscriptls + 1));
104 scriptls[nscriptls++] = t;
105 }
106
107 char *sanitize_symbol(char *symbol)
108 {
109 static char symbuf[2048];
110 char *sym = symbol;
111 char *tp = symbuf;
112
113 for (; *sym; sym++)
114 {
115 int c1 = *sym;
116 if (c1 == '\\')
117 {
118 *tp++ = '\\';
119 *tp++ = '\\';
120 continue;
121 }
122 if (c1 < 32 || c1 > 126)
123 {
124 int c;
125 *tp++ = '\\';
126 c = c1 >> 4;
127 c += 48;
128 if (c > 57)
129 c += 7;
130 *tp++ = c;
131 c = c1 & 15;
132 c += 48;
133 if (c > 57)
134 c += 7;
135 *tp++ = c;
136 continue;
137 }
138 *tp++ = c1;
139 }
140 *tp = 0;
141 return symbuf;
142 }