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