Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 1 | // Inferno utils/6l/obj.c |
| 2 | // http://code.google.com/p/inferno-os/source/browse/utils/6l/obj.c |
| 3 | // |
| 4 | // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved. |
| 5 | // Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net) |
| 6 | // Portions Copyright © 1997-1999 Vita Nuova Limited |
| 7 | // Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com) |
| 8 | // Portions Copyright © 2004,2006 Bruce Ellis |
| 9 | // Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net) |
| 10 | // Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others |
| 11 | // Portions Copyright © 2009 The Go Authors. All rights reserved. |
| 12 | // |
| 13 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | // of this software and associated documentation files (the "Software"), to deal |
| 15 | // in the Software without restriction, including without limitation the rights |
| 16 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | // copies of the Software, and to permit persons to whom the Software is |
| 18 | // furnished to do so, subject to the following conditions: |
| 19 | // |
| 20 | // The above copyright notice and this permission notice shall be included in |
| 21 | // all copies or substantial portions of the Software. |
| 22 | // |
| 23 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 29 | // THE SOFTWARE. |
| 30 | |
| 31 | #define EXTERN |
| 32 | #include "l.h" |
Russ Cox | c287497 | 2009-10-07 00:11:59 -0700 | [diff] [blame] | 33 | #include "../ld/lib.h" |
Russ Cox | 0e25c1d | 2009-08-21 13:08:11 -0700 | [diff] [blame] | 34 | #include "../ld/elf.h" |
Russ Cox | 2157025 | 2009-09-30 17:33:39 -0700 | [diff] [blame] | 35 | #include "../ld/macho.h" |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 36 | #include <ar.h> |
| 37 | |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 38 | char *noname = "<none>"; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 39 | char thechar = '6'; |
Ken Thompson | e2c3ec3 | 2008-06-05 16:56:23 -0700 | [diff] [blame] | 40 | char* thestring = "amd64"; |
| 41 | char* paramspace = "FP"; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * -H2 -T4136 -R4096 is plan9 64-bit format |
| 45 | * -H3 -T4128 -R4096 is plan9 32-bit format |
| 46 | * -H5 -T0x80110000 -R4096 is ELF32 |
| 47 | * -H6 -Tx -Rx is apple MH-exec |
Rob Pike | 073486c | 2008-06-15 17:22:57 -0700 | [diff] [blame] | 48 | * -H7 -Tx -Rx is linux elf-exec |
Devon H. O'Dell | 0489a26 | 2009-11-17 08:20:58 -0800 | [diff] [blame] | 49 | * -H9 -Tx -Rx is FreeBSD elf-exec |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 50 | * |
| 51 | * options used: 189BLQSWabcjlnpsvz |
| 52 | */ |
| 53 | |
| 54 | static int |
| 55 | isobjfile(char *f) |
| 56 | { |
| 57 | int n, v; |
| 58 | Biobuf *b; |
| 59 | char buf1[5], buf2[SARMAG]; |
| 60 | |
| 61 | b = Bopen(f, OREAD); |
| 62 | if(b == nil) |
| 63 | return 0; |
| 64 | n = Bread(b, buf1, 5); |
| 65 | if(n == 5 && (buf1[2] == 1 && buf1[3] == '<' || buf1[3] == 1 && buf1[4] == '<')) |
| 66 | v = 1; /* good enough for our purposes */ |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 67 | else { |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 68 | Bseek(b, 0, 0); |
| 69 | n = Bread(b, buf2, SARMAG); |
| 70 | v = n == SARMAG && strncmp(buf2, ARMAG, SARMAG) == 0; |
| 71 | } |
| 72 | Bterm(b); |
| 73 | return v; |
| 74 | } |
| 75 | |
| 76 | void |
Russ Cox | 48974f5 | 2009-04-30 13:32:39 -0700 | [diff] [blame] | 77 | usage(void) |
| 78 | { |
Ian Lance Taylor | e6443f6 | 2010-02-26 14:03:52 -0800 | [diff] [blame^] | 79 | fprint(2, "usage: 6l [-options] [-E entry] [-H head] [-L dir] [-T text] [-R rnd] [-r path] [-o out] main.6\n"); |
Russ Cox | 48974f5 | 2009-04-30 13:32:39 -0700 | [diff] [blame] | 80 | exits("usage"); |
| 81 | } |
| 82 | |
| 83 | void |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 84 | main(int argc, char *argv[]) |
| 85 | { |
| 86 | int i, c; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 87 | |
| 88 | Binit(&bso, 1, OWRITE); |
| 89 | cout = -1; |
| 90 | listinit(); |
| 91 | memset(debug, 0, sizeof(debug)); |
| 92 | nerrors = 0; |
| 93 | outfile = "6.out"; |
| 94 | HEADTYPE = -1; |
| 95 | INITTEXT = -1; |
| 96 | INITDAT = -1; |
| 97 | INITRND = -1; |
| 98 | INITENTRY = 0; |
Ken Thompson | e2c3ec3 | 2008-06-05 16:56:23 -0700 | [diff] [blame] | 99 | |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 100 | ARGBEGIN { |
| 101 | default: |
| 102 | c = ARGC(); |
Kai Backman | 2f1a321 | 2010-02-03 22:31:38 -0800 | [diff] [blame] | 103 | if(c == 'l') |
| 104 | usage(); |
| 105 | if(c >= 0 && c < sizeof(debug)) |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 106 | debug[c]++; |
| 107 | break; |
| 108 | case 'o': /* output to (next arg) */ |
Russ Cox | 48974f5 | 2009-04-30 13:32:39 -0700 | [diff] [blame] | 109 | outfile = EARGF(usage()); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 110 | break; |
| 111 | case 'E': |
Russ Cox | 48974f5 | 2009-04-30 13:32:39 -0700 | [diff] [blame] | 112 | INITENTRY = EARGF(usage()); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 113 | break; |
| 114 | case 'H': |
Russ Cox | 48974f5 | 2009-04-30 13:32:39 -0700 | [diff] [blame] | 115 | HEADTYPE = atolwhex(EARGF(usage())); |
| 116 | break; |
| 117 | case 'L': |
Russ Cox | c287497 | 2009-10-07 00:11:59 -0700 | [diff] [blame] | 118 | Lflag(EARGF(usage())); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 119 | break; |
| 120 | case 'T': |
Russ Cox | 48974f5 | 2009-04-30 13:32:39 -0700 | [diff] [blame] | 121 | INITTEXT = atolwhex(EARGF(usage())); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 122 | break; |
| 123 | case 'D': |
Russ Cox | 48974f5 | 2009-04-30 13:32:39 -0700 | [diff] [blame] | 124 | INITDAT = atolwhex(EARGF(usage())); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 125 | break; |
| 126 | case 'R': |
Russ Cox | 48974f5 | 2009-04-30 13:32:39 -0700 | [diff] [blame] | 127 | INITRND = atolwhex(EARGF(usage())); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 128 | break; |
Ian Lance Taylor | e6443f6 | 2010-02-26 14:03:52 -0800 | [diff] [blame^] | 129 | case 'r': |
| 130 | rpath = EARGF(usage()); |
| 131 | break; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 132 | case 'x': /* produce export table */ |
| 133 | doexp = 1; |
| 134 | if(argv[1] != nil && argv[1][0] != '-' && !isobjfile(argv[1])) |
| 135 | readundefs(ARGF(), SEXPORT); |
| 136 | break; |
| 137 | case 'u': /* produce dynamically loadable module */ |
| 138 | dlm = 1; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 139 | if(argv[1] != nil && argv[1][0] != '-' && !isobjfile(argv[1])) |
| 140 | readundefs(ARGF(), SIMPORT); |
| 141 | break; |
Andrew Gerrand | b4fb00b | 2010-02-08 09:46:53 -0800 | [diff] [blame] | 142 | case 'V': |
| 143 | print("%cl version %s\n", thechar, getgoversion()); |
| 144 | errorexit(); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 145 | } ARGEND |
Russ Cox | 3b1a035 | 2010-01-25 17:53:43 -0800 | [diff] [blame] | 146 | |
| 147 | if(argc != 1) |
Russ Cox | 07393f8 | 2009-06-02 22:33:21 -0700 | [diff] [blame] | 148 | usage(); |
Ken Thompson | e2c3ec3 | 2008-06-05 16:56:23 -0700 | [diff] [blame] | 149 | |
Russ Cox | c287497 | 2009-10-07 00:11:59 -0700 | [diff] [blame] | 150 | libinit(); |
Rob Pike | e6bc5bf | 2009-10-05 21:31:50 -0700 | [diff] [blame] | 151 | |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 152 | if(HEADTYPE == -1) { |
Ken Thompson | e2c3ec3 | 2008-06-05 16:56:23 -0700 | [diff] [blame] | 153 | HEADTYPE = 2; |
| 154 | if(strcmp(goos, "linux") == 0) |
| 155 | HEADTYPE = 7; |
| 156 | else |
| 157 | if(strcmp(goos, "darwin") == 0) |
| 158 | HEADTYPE = 6; |
| 159 | else |
Devon H. O'Dell | 60b1a17 | 2009-11-18 16:51:59 -0800 | [diff] [blame] | 160 | if(strcmp(goos, "freebsd") == 0) |
Devon H. O'Dell | 0489a26 | 2009-11-17 08:20:58 -0800 | [diff] [blame] | 161 | HEADTYPE = 9; |
Devon H. O'Dell | 60b1a17 | 2009-11-18 16:51:59 -0800 | [diff] [blame] | 162 | else |
Ken Thompson | e2c3ec3 | 2008-06-05 16:56:23 -0700 | [diff] [blame] | 163 | print("goos is not known: %s\n", goos); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 164 | } |
Ken Thompson | e2c3ec3 | 2008-06-05 16:56:23 -0700 | [diff] [blame] | 165 | |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 166 | switch(HEADTYPE) { |
| 167 | default: |
| 168 | diag("unknown -H option"); |
| 169 | errorexit(); |
| 170 | case 2: /* plan 9 */ |
| 171 | HEADR = 32L+8L; |
| 172 | if(INITTEXT == -1) |
| 173 | INITTEXT = 4096+HEADR; |
| 174 | if(INITDAT == -1) |
| 175 | INITDAT = 0; |
| 176 | if(INITRND == -1) |
| 177 | INITRND = 4096; |
| 178 | break; |
| 179 | case 3: /* plan 9 */ |
| 180 | HEADR = 32L; |
| 181 | if(INITTEXT == -1) |
| 182 | INITTEXT = 4096+32; |
| 183 | if(INITDAT == -1) |
| 184 | INITDAT = 0; |
| 185 | if(INITRND == -1) |
| 186 | INITRND = 4096; |
| 187 | break; |
| 188 | case 5: /* elf32 executable */ |
| 189 | HEADR = rnd(52L+3*32L, 16); |
| 190 | if(INITTEXT == -1) |
| 191 | INITTEXT = 0x80110000L; |
| 192 | if(INITDAT == -1) |
| 193 | INITDAT = 0; |
| 194 | if(INITRND == -1) |
| 195 | INITRND = 4096; |
| 196 | break; |
| 197 | case 6: /* apple MACH */ |
Russ Cox | 2157025 | 2009-09-30 17:33:39 -0700 | [diff] [blame] | 198 | machoinit(); |
| 199 | HEADR = MACHORESERVE; |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 200 | if(INITRND == -1) |
| 201 | INITRND = 4096; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 202 | if(INITTEXT == -1) |
| 203 | INITTEXT = 4096+HEADR; |
| 204 | if(INITDAT == -1) |
| 205 | INITDAT = 0; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 206 | break; |
Ken Thompson | e2c3ec3 | 2008-06-05 16:56:23 -0700 | [diff] [blame] | 207 | case 7: /* elf64 executable */ |
Devon H. O'Dell | 0489a26 | 2009-11-17 08:20:58 -0800 | [diff] [blame] | 208 | case 9: /* freebsd */ |
Russ Cox | 0e25c1d | 2009-08-21 13:08:11 -0700 | [diff] [blame] | 209 | elfinit(); |
| 210 | HEADR = ELFRESERVE; |
Ken Thompson | e2c3ec3 | 2008-06-05 16:56:23 -0700 | [diff] [blame] | 211 | if(INITTEXT == -1) |
Russ Cox | c3ca056 | 2008-09-08 15:22:55 -0700 | [diff] [blame] | 212 | INITTEXT = (1<<22)+HEADR; |
Ken Thompson | e2c3ec3 | 2008-06-05 16:56:23 -0700 | [diff] [blame] | 213 | if(INITDAT == -1) |
| 214 | INITDAT = 0; |
| 215 | if(INITRND == -1) |
| 216 | INITRND = 4096; |
| 217 | break; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 218 | } |
| 219 | if(INITDAT != 0 && INITRND != 0) |
| 220 | print("warning: -D0x%llux is ignored because of -R0x%lux\n", |
| 221 | INITDAT, INITRND); |
| 222 | if(debug['v']) |
| 223 | Bprint(&bso, "HEADER = -H%ld -T0x%llux -D0x%llux -R0x%lux\n", |
| 224 | HEADTYPE, INITTEXT, INITDAT, INITRND); |
| 225 | Bflush(&bso); |
| 226 | for(i=1; optab[i].as; i++) { |
| 227 | c = optab[i].as; |
| 228 | if(opindex[c] != nil) { |
| 229 | diag("phase error in optab: %d (%A)", i, c); |
| 230 | errorexit(); |
| 231 | } |
| 232 | opindex[c] = &optab[i]; |
| 233 | } |
| 234 | |
| 235 | for(i=0; i<Ymax; i++) |
| 236 | ycover[i*Ymax + i] = 1; |
| 237 | |
| 238 | ycover[Yi0*Ymax + Yi8] = 1; |
| 239 | ycover[Yi1*Ymax + Yi8] = 1; |
| 240 | |
| 241 | ycover[Yi0*Ymax + Ys32] = 1; |
| 242 | ycover[Yi1*Ymax + Ys32] = 1; |
| 243 | ycover[Yi8*Ymax + Ys32] = 1; |
| 244 | |
| 245 | ycover[Yi0*Ymax + Yi32] = 1; |
| 246 | ycover[Yi1*Ymax + Yi32] = 1; |
| 247 | ycover[Yi8*Ymax + Yi32] = 1; |
| 248 | ycover[Ys32*Ymax + Yi32] = 1; |
| 249 | |
| 250 | ycover[Yi0*Ymax + Yi64] = 1; |
| 251 | ycover[Yi1*Ymax + Yi64] = 1; |
| 252 | ycover[Yi8*Ymax + Yi64] = 1; |
| 253 | ycover[Ys32*Ymax + Yi64] = 1; |
| 254 | ycover[Yi32*Ymax + Yi64] = 1; |
| 255 | |
| 256 | ycover[Yal*Ymax + Yrb] = 1; |
| 257 | ycover[Ycl*Ymax + Yrb] = 1; |
| 258 | ycover[Yax*Ymax + Yrb] = 1; |
| 259 | ycover[Ycx*Ymax + Yrb] = 1; |
| 260 | ycover[Yrx*Ymax + Yrb] = 1; |
| 261 | ycover[Yrl*Ymax + Yrb] = 1; |
| 262 | |
| 263 | ycover[Ycl*Ymax + Ycx] = 1; |
| 264 | |
| 265 | ycover[Yax*Ymax + Yrx] = 1; |
| 266 | ycover[Ycx*Ymax + Yrx] = 1; |
| 267 | |
| 268 | ycover[Yax*Ymax + Yrl] = 1; |
| 269 | ycover[Ycx*Ymax + Yrl] = 1; |
| 270 | ycover[Yrx*Ymax + Yrl] = 1; |
| 271 | |
| 272 | ycover[Yf0*Ymax + Yrf] = 1; |
| 273 | |
| 274 | ycover[Yal*Ymax + Ymb] = 1; |
| 275 | ycover[Ycl*Ymax + Ymb] = 1; |
| 276 | ycover[Yax*Ymax + Ymb] = 1; |
| 277 | ycover[Ycx*Ymax + Ymb] = 1; |
| 278 | ycover[Yrx*Ymax + Ymb] = 1; |
| 279 | ycover[Yrb*Ymax + Ymb] = 1; |
| 280 | ycover[Yrl*Ymax + Ymb] = 1; |
| 281 | ycover[Ym*Ymax + Ymb] = 1; |
| 282 | |
| 283 | ycover[Yax*Ymax + Yml] = 1; |
| 284 | ycover[Ycx*Ymax + Yml] = 1; |
| 285 | ycover[Yrx*Ymax + Yml] = 1; |
| 286 | ycover[Yrl*Ymax + Yml] = 1; |
| 287 | ycover[Ym*Ymax + Yml] = 1; |
| 288 | |
| 289 | ycover[Yax*Ymax + Ymm] = 1; |
| 290 | ycover[Ycx*Ymax + Ymm] = 1; |
| 291 | ycover[Yrx*Ymax + Ymm] = 1; |
| 292 | ycover[Yrl*Ymax + Ymm] = 1; |
| 293 | ycover[Ym*Ymax + Ymm] = 1; |
| 294 | ycover[Ymr*Ymax + Ymm] = 1; |
| 295 | |
| 296 | ycover[Yax*Ymax + Yxm] = 1; |
| 297 | ycover[Ycx*Ymax + Yxm] = 1; |
| 298 | ycover[Yrx*Ymax + Yxm] = 1; |
| 299 | ycover[Yrl*Ymax + Yxm] = 1; |
| 300 | ycover[Ym*Ymax + Yxm] = 1; |
| 301 | ycover[Yxr*Ymax + Yxm] = 1; |
| 302 | |
| 303 | for(i=0; i<D_NONE; i++) { |
| 304 | reg[i] = -1; |
| 305 | if(i >= D_AL && i <= D_R15B) { |
| 306 | reg[i] = (i-D_AL) & 7; |
| 307 | if(i >= D_SPB && i <= D_DIB) |
| 308 | regrex[i] = 0x40; |
| 309 | if(i >= D_R8B && i <= D_R15B) |
| 310 | regrex[i] = Rxr | Rxx | Rxb; |
| 311 | } |
| 312 | if(i >= D_AH && i<= D_BH) |
| 313 | reg[i] = 4 + ((i-D_AH) & 7); |
| 314 | if(i >= D_AX && i <= D_R15) { |
| 315 | reg[i] = (i-D_AX) & 7; |
| 316 | if(i >= D_R8) |
| 317 | regrex[i] = Rxr | Rxx | Rxb; |
| 318 | } |
| 319 | if(i >= D_F0 && i <= D_F0+7) |
| 320 | reg[i] = (i-D_F0) & 7; |
| 321 | if(i >= D_M0 && i <= D_M0+7) |
| 322 | reg[i] = (i-D_M0) & 7; |
| 323 | if(i >= D_X0 && i <= D_X0+15) { |
| 324 | reg[i] = (i-D_X0) & 7; |
| 325 | if(i >= D_X0+8) |
| 326 | regrex[i] = Rxr | Rxx | Rxb; |
| 327 | } |
Ken Thompson | e2c3ec3 | 2008-06-05 16:56:23 -0700 | [diff] [blame] | 328 | if(i >= D_CR+8 && i <= D_CR+15) |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 329 | regrex[i] = Rxr; |
| 330 | } |
| 331 | |
| 332 | zprg.link = P; |
| 333 | zprg.pcond = P; |
| 334 | zprg.back = 2; |
| 335 | zprg.as = AGOK; |
| 336 | zprg.from.type = D_NONE; |
| 337 | zprg.from.index = D_NONE; |
| 338 | zprg.from.scale = 1; |
| 339 | zprg.to = zprg.from; |
| 340 | zprg.mode = 64; |
| 341 | |
| 342 | pcstr = "%.6llux "; |
| 343 | nuxiinit(); |
| 344 | histgen = 0; |
| 345 | textp = P; |
| 346 | datap = P; |
| 347 | edatap = P; |
| 348 | pc = 0; |
| 349 | dtype = 4; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 350 | version = 0; |
| 351 | cbp = buf.cbuf; |
| 352 | cbc = sizeof(buf.cbuf); |
| 353 | firstp = prg(); |
| 354 | lastp = firstp; |
| 355 | |
Russ Cox | 00f4c6a | 2010-02-03 16:30:45 -0800 | [diff] [blame] | 356 | addlibpath("command line", "command line", argv[0], "main"); |
Kai Backman | 2f1a321 | 2010-02-03 22:31:38 -0800 | [diff] [blame] | 357 | loadlib(); |
Russ Cox | c287497 | 2009-10-07 00:11:59 -0700 | [diff] [blame] | 358 | |
Russ Cox | c3fa54c | 2009-01-21 14:50:27 -0800 | [diff] [blame] | 359 | deadcode(); |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 360 | |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 361 | firstp = firstp->link; |
| 362 | if(firstp == P) |
| 363 | errorexit(); |
Ken Thompson | e2c3ec3 | 2008-06-05 16:56:23 -0700 | [diff] [blame] | 364 | |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 365 | if(doexp || dlm){ |
| 366 | EXPTAB = "_exporttab"; |
| 367 | zerosig(EXPTAB); |
| 368 | zerosig("etext"); |
| 369 | zerosig("edata"); |
| 370 | zerosig("end"); |
| 371 | if(dlm){ |
| 372 | import(); |
| 373 | HEADTYPE = 2; |
| 374 | INITTEXT = 0; |
| 375 | INITDAT = 0; |
| 376 | INITRND = 8; |
| 377 | INITENTRY = EXPTAB; |
| 378 | } |
| 379 | export(); |
| 380 | } |
Ken Thompson | e2c3ec3 | 2008-06-05 16:56:23 -0700 | [diff] [blame] | 381 | |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 382 | patch(); |
| 383 | follow(); |
Russ Cox | 0e25c1d | 2009-08-21 13:08:11 -0700 | [diff] [blame] | 384 | doelf(); |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 385 | if(HEADTYPE == 6) |
| 386 | domacho(); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 387 | dodata(); |
Russ Cox | bd4161f | 2009-08-20 16:09:38 -0700 | [diff] [blame] | 388 | dobss(); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 389 | dostkoff(); |
| 390 | paramspace = "SP"; /* (FP) now (SP) on output */ |
| 391 | if(debug['p']) |
| 392 | if(debug['1']) |
| 393 | doprof1(); |
| 394 | else |
| 395 | doprof2(); |
| 396 | span(); |
| 397 | doinit(); |
| 398 | asmb(); |
| 399 | undef(); |
| 400 | if(debug['v']) { |
| 401 | Bprint(&bso, "%5.2f cpu time\n", cputime()); |
| 402 | Bprint(&bso, "%ld symbols\n", nsymbol); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 403 | Bprint(&bso, "%d sizeof adr\n", sizeof(Adr)); |
| 404 | Bprint(&bso, "%d sizeof prog\n", sizeof(Prog)); |
| 405 | } |
| 406 | Bflush(&bso); |
| 407 | |
| 408 | errorexit(); |
| 409 | } |
| 410 | |
| 411 | void |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 412 | zaddr(Biobuf *f, Adr *a, Sym *h[]) |
| 413 | { |
| 414 | int t; |
Russ Cox | 9aad9fe | 2008-08-03 17:25:15 -0700 | [diff] [blame] | 415 | int32 l; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 416 | Sym *s; |
| 417 | Auto *u; |
| 418 | |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 419 | t = Bgetc(f); |
Ken Thompson | 9c75354 | 2009-11-04 18:17:57 -0800 | [diff] [blame] | 420 | a->index = D_NONE; |
| 421 | a->scale = 0; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 422 | if(t & T_INDEX) { |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 423 | a->index = Bgetc(f); |
| 424 | a->scale = Bgetc(f); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 425 | } |
| 426 | a->offset = 0; |
| 427 | if(t & T_OFFSET) { |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 428 | a->offset = Bget4(f); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 429 | if(t & T_64) { |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 430 | a->offset &= 0xFFFFFFFFULL; |
| 431 | a->offset |= (vlong)Bget4(f) << 32; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | a->sym = S; |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 435 | if(t & T_SYM) |
| 436 | a->sym = h[Bgetc(f)]; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 437 | a->type = D_NONE; |
| 438 | if(t & T_FCONST) { |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 439 | a->ieee.l = Bget4(f); |
| 440 | a->ieee.h = Bget4(f); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 441 | a->type = D_FCONST; |
| 442 | } else |
| 443 | if(t & T_SCONST) { |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 444 | Bread(f, a->scon, NSNAME); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 445 | a->type = D_SCONST; |
| 446 | } |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 447 | if(t & T_TYPE) |
| 448 | a->type = Bgetc(f); |
Ken Thompson | 9c75354 | 2009-11-04 18:17:57 -0800 | [diff] [blame] | 449 | adrgotype = S; |
Russ Cox | 43f29e6 | 2009-08-12 18:16:24 -0700 | [diff] [blame] | 450 | if(t & T_GOTYPE) |
Ken Thompson | 9c75354 | 2009-11-04 18:17:57 -0800 | [diff] [blame] | 451 | adrgotype = h[Bgetc(f)]; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 452 | s = a->sym; |
| 453 | if(s == S) |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 454 | return; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 455 | |
| 456 | t = a->type; |
Russ Cox | 311c0b4 | 2009-08-13 14:41:42 -0700 | [diff] [blame] | 457 | if(t != D_AUTO && t != D_PARAM) { |
Ken Thompson | 9c75354 | 2009-11-04 18:17:57 -0800 | [diff] [blame] | 458 | if(adrgotype) |
| 459 | s->gotype = adrgotype; |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 460 | return; |
Russ Cox | 311c0b4 | 2009-08-13 14:41:42 -0700 | [diff] [blame] | 461 | } |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 462 | l = a->offset; |
| 463 | for(u=curauto; u; u=u->link) { |
| 464 | if(u->asym == s) |
| 465 | if(u->type == t) { |
| 466 | if(u->aoffset > l) |
| 467 | u->aoffset = l; |
Ken Thompson | 9c75354 | 2009-11-04 18:17:57 -0800 | [diff] [blame] | 468 | if(adrgotype) |
| 469 | u->gotype = adrgotype; |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 470 | return; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
| 474 | u = mal(sizeof(*u)); |
| 475 | u->link = curauto; |
| 476 | curauto = u; |
| 477 | u->asym = s; |
| 478 | u->aoffset = l; |
| 479 | u->type = t; |
Ken Thompson | 9c75354 | 2009-11-04 18:17:57 -0800 | [diff] [blame] | 480 | u->gotype = adrgotype; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 484 | nopout(Prog *p) |
| 485 | { |
| 486 | p->as = ANOP; |
| 487 | p->from.type = D_NONE; |
| 488 | p->to.type = D_NONE; |
| 489 | } |
| 490 | |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 491 | void |
Russ Cox | 758f2bc | 2010-01-22 17:06:20 -0800 | [diff] [blame] | 492 | ldobj1(Biobuf *f, char *pkg, int64 len, char *pn) |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 493 | { |
| 494 | vlong ipc; |
| 495 | Prog *p, *t; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 496 | int v, o, r, skip, mode; |
| 497 | Sym *h[NSYM], *s, *di; |
Russ Cox | 9aad9fe | 2008-08-03 17:25:15 -0700 | [diff] [blame] | 498 | uint32 sig; |
Russ Cox | 758f2bc | 2010-01-22 17:06:20 -0800 | [diff] [blame] | 499 | char *name, *x; |
Russ Cox | c287497 | 2009-10-07 00:11:59 -0700 | [diff] [blame] | 500 | int ntext; |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 501 | vlong eof; |
Russ Cox | c5f21c0 | 2008-11-13 13:42:26 -0800 | [diff] [blame] | 502 | char src[1024]; |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 503 | |
Rob Pike | d5f4d94 | 2008-07-09 16:40:11 -0700 | [diff] [blame] | 504 | ntext = 0; |
Russ Cox | c287497 | 2009-10-07 00:11:59 -0700 | [diff] [blame] | 505 | eof = Boffset(f) + len; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 506 | di = S; |
Russ Cox | c287497 | 2009-10-07 00:11:59 -0700 | [diff] [blame] | 507 | src[0] = 0; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 508 | |
| 509 | newloop: |
| 510 | memset(h, 0, sizeof(h)); |
| 511 | version++; |
| 512 | histfrogp = 0; |
| 513 | ipc = pc; |
| 514 | skip = 0; |
| 515 | mode = 64; |
| 516 | |
| 517 | loop: |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 518 | if(f->state == Bracteof || Boffset(f) >= eof) |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 519 | goto eof; |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 520 | o = Bgetc(f); |
| 521 | if(o == Beof) |
| 522 | goto eof; |
| 523 | o |= Bgetc(f) << 8; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 524 | if(o <= AXXX || o >= ALAST) { |
| 525 | if(o < 0) |
| 526 | goto eof; |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 527 | diag("%s:#%lld: opcode out of range: %#ux", pn, Boffset(f), o); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 528 | print(" probably not a .6 file\n"); |
| 529 | errorexit(); |
| 530 | } |
| 531 | |
| 532 | if(o == ANAME || o == ASIGNAME) { |
| 533 | sig = 0; |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 534 | if(o == ASIGNAME) |
| 535 | sig = Bget4(f); |
| 536 | v = Bgetc(f); /* type */ |
| 537 | o = Bgetc(f); /* sym */ |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 538 | r = 0; |
| 539 | if(v == D_STATIC) |
| 540 | r = version; |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 541 | name = Brdline(f, '\0'); |
| 542 | if(name == nil) { |
| 543 | if(Blinelen(f) > 0) { |
| 544 | fprint(2, "%s: name too long\n", pn); |
| 545 | errorexit(); |
| 546 | } |
| 547 | goto eof; |
| 548 | } |
Russ Cox | 758f2bc | 2010-01-22 17:06:20 -0800 | [diff] [blame] | 549 | x = expandpkg(name, pkg); |
| 550 | s = lookup(x, r); |
| 551 | if(x != name) |
| 552 | free(x); |
| 553 | name = nil; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 554 | |
| 555 | if(debug['S'] && r == 0) |
| 556 | sig = 1729; |
| 557 | if(sig != 0){ |
| 558 | if(s->sig != 0 && s->sig != sig) |
Ken Thompson | ddba96a | 2008-06-18 22:07:09 -0700 | [diff] [blame] | 559 | diag("incompatible type signatures" |
| 560 | "%lux(%s) and %lux(%s) for %s", |
Russ Cox | c287497 | 2009-10-07 00:11:59 -0700 | [diff] [blame] | 561 | s->sig, s->file, sig, pn, s->name); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 562 | s->sig = sig; |
Russ Cox | c287497 | 2009-10-07 00:11:59 -0700 | [diff] [blame] | 563 | s->file = pn; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | if(debug['W']) |
| 567 | print(" ANAME %s\n", s->name); |
| 568 | h[o] = s; |
Russ Cox | fc8dca9 | 2009-02-05 13:58:43 -0800 | [diff] [blame] | 569 | if((v == D_EXTERN || v == D_STATIC) && s->type == 0) |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 570 | s->type = SXREF; |
| 571 | if(v == D_FILE) { |
| 572 | if(s->type != SFILE) { |
| 573 | histgen++; |
| 574 | s->type = SFILE; |
| 575 | s->value = histgen; |
| 576 | } |
| 577 | if(histfrogp < MAXHIST) { |
| 578 | histfrog[histfrogp] = s; |
| 579 | histfrogp++; |
| 580 | } else |
| 581 | collapsefrog(s); |
| 582 | } |
| 583 | goto loop; |
| 584 | } |
| 585 | |
| 586 | p = mal(sizeof(*p)); |
| 587 | p->as = o; |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 588 | p->line = Bget4(f); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 589 | p->back = 2; |
| 590 | p->mode = mode; |
Ken Thompson | 9c75354 | 2009-11-04 18:17:57 -0800 | [diff] [blame] | 591 | p->ft = 0; |
| 592 | p->tt = 0; |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 593 | zaddr(f, &p->from, h); |
Ken Thompson | 9c75354 | 2009-11-04 18:17:57 -0800 | [diff] [blame] | 594 | fromgotype = adrgotype; |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 595 | zaddr(f, &p->to, h); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 596 | |
| 597 | if(debug['W']) |
| 598 | print("%P\n", p); |
| 599 | |
| 600 | switch(p->as) { |
| 601 | case AHISTORY: |
| 602 | if(p->to.offset == -1) { |
Russ Cox | c5f21c0 | 2008-11-13 13:42:26 -0800 | [diff] [blame] | 603 | addlib(src, pn); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 604 | histfrogp = 0; |
| 605 | goto loop; |
| 606 | } |
Russ Cox | c5f21c0 | 2008-11-13 13:42:26 -0800 | [diff] [blame] | 607 | if(src[0] == '\0') |
| 608 | copyhistfrog(src, sizeof src); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 609 | addhist(p->line, D_FILE); /* 'z' */ |
| 610 | if(p->to.offset) |
| 611 | addhist(p->to.offset, D_FILE1); /* 'Z' */ |
| 612 | histfrogp = 0; |
| 613 | goto loop; |
| 614 | |
| 615 | case AEND: |
| 616 | histtoauto(); |
| 617 | if(curtext != P) |
| 618 | curtext->to.autom = curauto; |
| 619 | curauto = 0; |
| 620 | curtext = P; |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 621 | if(Boffset(f) == eof) |
| 622 | return; |
| 623 | goto newloop; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 624 | |
| 625 | case AGLOBL: |
| 626 | s = p->from.sym; |
| 627 | if(s->type == 0 || s->type == SXREF) { |
| 628 | s->type = SBSS; |
| 629 | s->value = 0; |
| 630 | } |
| 631 | if(s->type != SBSS) { |
| 632 | diag("%s: redefinition: %s in %s", |
| 633 | pn, s->name, TNAME); |
| 634 | s->type = SBSS; |
| 635 | s->value = 0; |
| 636 | } |
| 637 | if(p->to.offset > s->value) |
| 638 | s->value = p->to.offset; |
Russ Cox | 1983121 | 2008-11-05 11:27:50 -0800 | [diff] [blame] | 639 | if(p->from.scale & DUPOK) |
| 640 | s->dupok = 1; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 641 | goto loop; |
| 642 | |
| 643 | case ADYNT: |
| 644 | if(p->to.sym == S) { |
| 645 | diag("DYNT without a sym\n%P", p); |
| 646 | break; |
| 647 | } |
| 648 | di = p->to.sym; |
| 649 | p->from.scale = 4; |
| 650 | if(di->type == SXREF) { |
| 651 | if(debug['z']) |
| 652 | Bprint(&bso, "%P set to %d\n", p, dtype); |
| 653 | di->type = SCONST; |
| 654 | di->value = dtype; |
| 655 | dtype += 4; |
| 656 | } |
| 657 | if(p->from.sym == S) |
| 658 | break; |
| 659 | |
| 660 | p->from.offset = di->value; |
| 661 | p->from.sym->type = SDATA; |
| 662 | if(curtext == P) { |
| 663 | diag("DYNT not in text: %P", p); |
| 664 | break; |
| 665 | } |
| 666 | p->to.sym = curtext->from.sym; |
| 667 | p->to.type = D_ADDR; |
| 668 | p->to.index = D_EXTERN; |
| 669 | goto data; |
| 670 | |
| 671 | case AINIT: |
| 672 | if(p->from.sym == S) { |
| 673 | diag("INIT without a sym\n%P", p); |
| 674 | break; |
| 675 | } |
| 676 | if(di == S) { |
| 677 | diag("INIT without previous DYNT\n%P", p); |
| 678 | break; |
| 679 | } |
| 680 | p->from.offset = di->value; |
| 681 | p->from.sym->type = SDATA; |
| 682 | goto data; |
| 683 | |
| 684 | case ADATA: |
| 685 | data: |
Russ Cox | 1983121 | 2008-11-05 11:27:50 -0800 | [diff] [blame] | 686 | // Assume that AGLOBL comes after ADATA. |
| 687 | // If we've seen an AGLOBL that said this sym was DUPOK, |
| 688 | // ignore any more ADATA we see, which must be |
| 689 | // redefinitions. |
Russ Cox | c3fa54c | 2009-01-21 14:50:27 -0800 | [diff] [blame] | 690 | s = p->from.sym; |
| 691 | if(s != S && s->dupok) { |
Ken Thompson | 9c75354 | 2009-11-04 18:17:57 -0800 | [diff] [blame] | 692 | // if(debug['v']) |
| 693 | // Bprint(&bso, "skipping %s in %s: dupok\n", s->name, pn); |
Russ Cox | 1983121 | 2008-11-05 11:27:50 -0800 | [diff] [blame] | 694 | goto loop; |
Russ Cox | 8f14451 | 2009-01-20 13:21:22 -0800 | [diff] [blame] | 695 | } |
Russ Cox | c3fa54c | 2009-01-21 14:50:27 -0800 | [diff] [blame] | 696 | if(s != S) { |
| 697 | p->dlink = s->data; |
| 698 | s->data = p; |
Russ Cox | 69e244a | 2010-01-25 08:53:27 -0800 | [diff] [blame] | 699 | if(s->file == nil) |
| 700 | s->file = pn; |
| 701 | else if(s->file != pn) { |
| 702 | diag("multiple initialization for %s: in both %s and %s", s->name, s->file, pn); |
| 703 | errorexit(); |
| 704 | } |
Russ Cox | c3fa54c | 2009-01-21 14:50:27 -0800 | [diff] [blame] | 705 | } |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 706 | if(edatap == P) |
| 707 | datap = p; |
| 708 | else |
| 709 | edatap->link = p; |
| 710 | edatap = p; |
| 711 | p->link = P; |
| 712 | goto loop; |
| 713 | |
| 714 | case AGOK: |
| 715 | diag("%s: GOK opcode in %s", pn, TNAME); |
| 716 | pc++; |
| 717 | goto loop; |
| 718 | |
| 719 | case ATEXT: |
Rob Pike | d5f4d94 | 2008-07-09 16:40:11 -0700 | [diff] [blame] | 720 | s = p->from.sym; |
Russ Cox | fc8dca9 | 2009-02-05 13:58:43 -0800 | [diff] [blame] | 721 | if(ntext++ == 0 && s->type != 0 && s->type != SXREF) { |
Rob Pike | d5f4d94 | 2008-07-09 16:40:11 -0700 | [diff] [blame] | 722 | /* redefinition, so file has probably been seen before */ |
| 723 | if(debug['v']) |
Russ Cox | 8f14451 | 2009-01-20 13:21:22 -0800 | [diff] [blame] | 724 | Bprint(&bso, "skipping: %s: redefinition: %s", pn, s->name); |
Rob Pike | d5f4d94 | 2008-07-09 16:40:11 -0700 | [diff] [blame] | 725 | return; |
| 726 | } |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 727 | if(curtext != P) { |
| 728 | histtoauto(); |
| 729 | curtext->to.autom = curauto; |
| 730 | curauto = 0; |
| 731 | } |
| 732 | skip = 0; |
| 733 | curtext = p; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 734 | if(s == S) { |
| 735 | diag("%s: no TEXT symbol: %P", pn, p); |
| 736 | errorexit(); |
| 737 | } |
Russ Cox | fc8dca9 | 2009-02-05 13:58:43 -0800 | [diff] [blame] | 738 | if(s->type != 0 && s->type != SXREF) { |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 739 | if(p->from.scale & DUPOK) { |
| 740 | skip = 1; |
| 741 | goto casdef; |
| 742 | } |
| 743 | diag("%s: redefinition: %s\n%P", pn, s->name, p); |
| 744 | } |
Ken Thompson | 9c75354 | 2009-11-04 18:17:57 -0800 | [diff] [blame] | 745 | if(fromgotype) { |
| 746 | if(s->gotype && s->gotype != fromgotype) |
Russ Cox | 311c0b4 | 2009-08-13 14:41:42 -0700 | [diff] [blame] | 747 | diag("%s: type mismatch for %s", pn, s->name); |
Ken Thompson | 9c75354 | 2009-11-04 18:17:57 -0800 | [diff] [blame] | 748 | s->gotype = fromgotype; |
Russ Cox | 311c0b4 | 2009-08-13 14:41:42 -0700 | [diff] [blame] | 749 | } |
Russ Cox | a3c4faf | 2009-01-20 15:36:43 -0800 | [diff] [blame] | 750 | newtext(p, s); |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 751 | goto loop; |
| 752 | |
| 753 | case AMODE: |
| 754 | if(p->from.type == D_CONST || p->from.type == D_INDIR+D_NONE){ |
| 755 | switch((int)p->from.offset){ |
| 756 | case 16: case 32: case 64: |
| 757 | mode = p->from.offset; |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | goto loop; |
| 762 | |
| 763 | case AFMOVF: |
| 764 | case AFADDF: |
| 765 | case AFSUBF: |
| 766 | case AFSUBRF: |
| 767 | case AFMULF: |
| 768 | case AFDIVF: |
| 769 | case AFDIVRF: |
| 770 | case AFCOMF: |
| 771 | case AFCOMFP: |
| 772 | case AMOVSS: |
| 773 | case AADDSS: |
| 774 | case ASUBSS: |
| 775 | case AMULSS: |
| 776 | case ADIVSS: |
| 777 | case ACOMISS: |
| 778 | case AUCOMISS: |
| 779 | if(skip) |
| 780 | goto casdef; |
| 781 | if(p->from.type == D_FCONST) { |
| 782 | /* size sb 9 max */ |
| 783 | sprint(literal, "$%lux", ieeedtof(&p->from.ieee)); |
| 784 | s = lookup(literal, 0); |
| 785 | if(s->type == 0) { |
| 786 | s->type = SBSS; |
| 787 | s->value = 4; |
| 788 | t = prg(); |
| 789 | t->as = ADATA; |
| 790 | t->line = p->line; |
| 791 | t->from.type = D_EXTERN; |
| 792 | t->from.sym = s; |
| 793 | t->from.scale = 4; |
| 794 | t->to = p->from; |
| 795 | if(edatap == P) |
| 796 | datap = t; |
| 797 | else |
| 798 | edatap->link = t; |
| 799 | edatap = t; |
| 800 | t->link = P; |
| 801 | } |
| 802 | p->from.type = D_EXTERN; |
| 803 | p->from.sym = s; |
| 804 | p->from.offset = 0; |
| 805 | } |
| 806 | goto casdef; |
| 807 | |
| 808 | case AFMOVD: |
| 809 | case AFADDD: |
| 810 | case AFSUBD: |
| 811 | case AFSUBRD: |
| 812 | case AFMULD: |
| 813 | case AFDIVD: |
| 814 | case AFDIVRD: |
| 815 | case AFCOMD: |
| 816 | case AFCOMDP: |
| 817 | case AMOVSD: |
| 818 | case AADDSD: |
| 819 | case ASUBSD: |
| 820 | case AMULSD: |
| 821 | case ADIVSD: |
| 822 | case ACOMISD: |
| 823 | case AUCOMISD: |
| 824 | if(skip) |
| 825 | goto casdef; |
| 826 | if(p->from.type == D_FCONST) { |
| 827 | /* size sb 18 max */ |
| 828 | sprint(literal, "$%lux.%lux", |
| 829 | p->from.ieee.l, p->from.ieee.h); |
| 830 | s = lookup(literal, 0); |
| 831 | if(s->type == 0) { |
| 832 | s->type = SBSS; |
| 833 | s->value = 8; |
| 834 | t = prg(); |
| 835 | t->as = ADATA; |
| 836 | t->line = p->line; |
| 837 | t->from.type = D_EXTERN; |
| 838 | t->from.sym = s; |
| 839 | t->from.scale = 8; |
| 840 | t->to = p->from; |
| 841 | if(edatap == P) |
| 842 | datap = t; |
| 843 | else |
| 844 | edatap->link = t; |
| 845 | edatap = t; |
| 846 | t->link = P; |
| 847 | } |
| 848 | p->from.type = D_EXTERN; |
| 849 | p->from.sym = s; |
| 850 | p->from.offset = 0; |
| 851 | } |
| 852 | goto casdef; |
| 853 | |
| 854 | casdef: |
| 855 | default: |
| 856 | if(skip) |
| 857 | nopout(p); |
| 858 | |
| 859 | if(p->to.type == D_BRANCH) |
| 860 | p->to.offset += ipc; |
| 861 | lastp->link = p; |
| 862 | lastp = p; |
| 863 | p->pc = pc; |
| 864 | pc++; |
| 865 | goto loop; |
| 866 | } |
| 867 | goto loop; |
| 868 | |
| 869 | eof: |
| 870 | diag("truncated object file: %s", pn); |
| 871 | } |
| 872 | |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 873 | Prog* |
| 874 | prg(void) |
| 875 | { |
| 876 | Prog *p; |
| 877 | |
| 878 | p = mal(sizeof(*p)); |
| 879 | |
| 880 | *p = zprg; |
| 881 | return p; |
| 882 | } |
| 883 | |
| 884 | Prog* |
| 885 | copyp(Prog *q) |
| 886 | { |
| 887 | Prog *p; |
| 888 | |
| 889 | p = prg(); |
| 890 | *p = *q; |
| 891 | return p; |
| 892 | } |
| 893 | |
| 894 | Prog* |
| 895 | appendp(Prog *q) |
| 896 | { |
| 897 | Prog *p; |
| 898 | |
| 899 | p = prg(); |
| 900 | p->link = q->link; |
| 901 | q->link = p; |
| 902 | p->line = q->line; |
| 903 | p->mode = q->mode; |
| 904 | return p; |
| 905 | } |
| 906 | |
| 907 | void |
| 908 | doprof1(void) |
| 909 | { |
| 910 | Sym *s; |
Russ Cox | 9aad9fe | 2008-08-03 17:25:15 -0700 | [diff] [blame] | 911 | int32 n; |
Rob Pike | 0cafb9e | 2008-06-04 14:37:38 -0700 | [diff] [blame] | 912 | Prog *p, *q; |
| 913 | |
| 914 | if(debug['v']) |
| 915 | Bprint(&bso, "%5.2f profile 1\n", cputime()); |
| 916 | Bflush(&bso); |
| 917 | s = lookup("__mcount", 0); |
| 918 | n = 1; |
| 919 | for(p = firstp->link; p != P; p = p->link) { |
| 920 | if(p->as == ATEXT) { |
| 921 | q = prg(); |
| 922 | q->line = p->line; |
| 923 | q->link = datap; |
| 924 | datap = q; |
| 925 | q->as = ADATA; |
| 926 | q->from.type = D_EXTERN; |
| 927 | q->from.offset = n*4; |
| 928 | q->from.sym = s; |
| 929 | q->from.scale = 4; |
| 930 | q->to = p->from; |
| 931 | q->to.type = D_CONST; |
| 932 | |
| 933 | q = prg(); |
| 934 | q->line = p->line; |
| 935 | q->pc = p->pc; |
| 936 | q->link = p->link; |
| 937 | p->link = q; |
| 938 | p = q; |
| 939 | p->as = AADDL; |
| 940 | p->from.type = D_CONST; |
| 941 | p->from.offset = 1; |
| 942 | p->to.type = D_EXTERN; |
| 943 | p->to.sym = s; |
| 944 | p->to.offset = n*4 + 4; |
| 945 | |
| 946 | n += 2; |
| 947 | continue; |
| 948 | } |
| 949 | } |
| 950 | q = prg(); |
| 951 | q->line = 0; |
| 952 | q->link = datap; |
| 953 | datap = q; |
| 954 | |
| 955 | q->as = ADATA; |
| 956 | q->from.type = D_EXTERN; |
| 957 | q->from.sym = s; |
| 958 | q->from.scale = 4; |
| 959 | q->to.type = D_CONST; |
| 960 | q->to.offset = n; |
| 961 | |
| 962 | s->type = SBSS; |
| 963 | s->value = n*4; |
| 964 | } |
| 965 | |
| 966 | void |
| 967 | doprof2(void) |
| 968 | { |
| 969 | Sym *s2, *s4; |
| 970 | Prog *p, *q, *ps2, *ps4; |
| 971 | |
| 972 | if(debug['v']) |
| 973 | Bprint(&bso, "%5.2f profile 2\n", cputime()); |
| 974 | Bflush(&bso); |
| 975 | |
| 976 | s2 = lookup("_profin", 0); |
| 977 | s4 = lookup("_profout", 0); |
| 978 | if(s2->type != STEXT || s4->type != STEXT) { |
| 979 | diag("_profin/_profout not defined"); |
| 980 | return; |
| 981 | } |
| 982 | |
| 983 | ps2 = P; |
| 984 | ps4 = P; |
| 985 | for(p = firstp; p != P; p = p->link) { |
| 986 | if(p->as == ATEXT) { |
| 987 | if(p->from.sym == s2) { |
| 988 | p->from.scale = 1; |
| 989 | ps2 = p; |
| 990 | } |
| 991 | if(p->from.sym == s4) { |
| 992 | p->from.scale = 1; |
| 993 | ps4 = p; |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | for(p = firstp; p != P; p = p->link) { |
| 998 | if(p->as == ATEXT) { |
| 999 | curtext = p; |
| 1000 | |
| 1001 | if(p->from.scale & NOPROF) { /* dont profile */ |
| 1002 | for(;;) { |
| 1003 | q = p->link; |
| 1004 | if(q == P) |
| 1005 | break; |
| 1006 | if(q->as == ATEXT) |
| 1007 | break; |
| 1008 | p = q; |
| 1009 | } |
| 1010 | continue; |
| 1011 | } |
| 1012 | |
| 1013 | /* |
| 1014 | * JMPL profin |
| 1015 | */ |
| 1016 | q = prg(); |
| 1017 | q->line = p->line; |
| 1018 | q->pc = p->pc; |
| 1019 | q->link = p->link; |
| 1020 | p->link = q; |
| 1021 | p = q; |
| 1022 | p->as = ACALL; |
| 1023 | p->to.type = D_BRANCH; |
| 1024 | p->pcond = ps2; |
| 1025 | p->to.sym = s2; |
| 1026 | |
| 1027 | continue; |
| 1028 | } |
| 1029 | if(p->as == ARET) { |
| 1030 | /* |
| 1031 | * RET |
| 1032 | */ |
| 1033 | q = prg(); |
| 1034 | q->as = ARET; |
| 1035 | q->from = p->from; |
| 1036 | q->to = p->to; |
| 1037 | q->link = p->link; |
| 1038 | p->link = q; |
| 1039 | |
| 1040 | /* |
| 1041 | * JAL profout |
| 1042 | */ |
| 1043 | p->as = ACALL; |
| 1044 | p->from = zprg.from; |
| 1045 | p->to = zprg.to; |
| 1046 | p->to.type = D_BRANCH; |
| 1047 | p->pcond = ps4; |
| 1048 | p->to.sym = s4; |
| 1049 | |
| 1050 | p = q; |
| 1051 | |
| 1052 | continue; |
| 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | |