Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/tools/hsdis/hsdis.c
32285 views
1
/*
2
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* The Universal Permissive License (UPL), Version 1.0
6
*
7
* Subject to the condition set forth below, permission is hereby granted to
8
* any person obtaining a copy of this software, associated documentation
9
* and/or data (collectively the "Software"), free of charge and under any
10
* and all copyright rights in the Software, and any and all patent rights
11
* owned or freely licensable by each licensor hereunder covering either (i)
12
* the unmodified Software as contributed to or provided by such licensor,
13
* or (ii) the Larger Works (as defined below), to deal in both
14
*
15
* (a) the Software, and
16
*
17
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file
18
* if one is included with the Software (each a "Larger Work" to which the
19
* Software is contributed by such licensors),
20
*
21
* without restriction, including without limitation the rights to copy,
22
* create derivative works of, display, perform, and distribute the Software
23
* and make, use, sell, offer for sale, import, export, have made, and have
24
* sold the Software and the Larger Work(s), and to sublicense the foregoing
25
* rights on either these or other terms.
26
*
27
* This license is subject to the following condition:
28
*
29
* The above copyright notice and either this complete permission notice or
30
* at a minimum a reference to the UPL must be included in all copies or
31
* substantial portions of the Software.
32
*
33
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
34
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
35
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
36
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
37
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
38
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
39
* USE OR OTHER DEALINGS IN THE SOFTWARE.
40
*
41
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
42
* or visit www.oracle.com if you need additional information or have any
43
* questions.
44
*
45
*/
46
47
/* hsdis.c -- dump a range of addresses as native instructions
48
This implements the plugin protocol required by the
49
HotSpot PrintAssembly option.
50
*/
51
52
#include <config.h> /* required by bfd.h */
53
#include <errno.h>
54
#include <inttypes.h>
55
#include <string.h>
56
57
#include <libiberty.h>
58
#include <bfd.h>
59
#include <bfdver.h>
60
#include <dis-asm.h>
61
62
#include "hsdis.h"
63
64
#ifndef bool
65
#define bool int
66
#define true 1
67
#define false 0
68
#endif /*bool*/
69
70
/* short names for stuff in hsdis.h */
71
typedef decode_instructions_event_callback_ftype event_callback_t;
72
typedef decode_instructions_printf_callback_ftype printf_callback_t;
73
74
/* disassemble_info.application_data object */
75
struct hsdis_app_data {
76
/* virtual address of data */
77
uintptr_t start_va, end_va;
78
/* the instructions to be decoded */
79
unsigned char* buffer;
80
uintptr_t length;
81
event_callback_t event_callback; void* event_stream;
82
printf_callback_t printf_callback; void* printf_stream;
83
bool losing;
84
bool do_newline;
85
86
/* the architecture being disassembled */
87
const char* arch_name;
88
const bfd_arch_info_type* arch_info;
89
90
/* the disassembler we are going to use: */
91
disassembler_ftype dfn;
92
struct disassemble_info dinfo; /* the actual struct! */
93
94
char mach_option[64];
95
char insn_options[256];
96
};
97
98
static void* decode(struct hsdis_app_data* app_data, const char* options);
99
100
#define DECL_APP_DATA(dinfo) \
101
struct hsdis_app_data* app_data = (struct hsdis_app_data*) (dinfo)->application_data
102
103
#define DECL_EVENT_CALLBACK(app_data) \
104
event_callback_t event_callback = (app_data)->event_callback; \
105
void* event_stream = (app_data)->event_stream
106
107
#define DECL_PRINTF_CALLBACK(app_data) \
108
printf_callback_t printf_callback = (app_data)->printf_callback; \
109
void* printf_stream = (app_data)->printf_stream
110
111
112
static void print_help(struct hsdis_app_data* app_data,
113
const char* msg, const char* arg);
114
static void setup_app_data(struct hsdis_app_data* app_data,
115
const char* options);
116
static const char* format_insn_close(const char* close,
117
disassemble_info* dinfo,
118
char* buf, size_t bufsize);
119
120
void*
121
#ifdef DLL_ENTRY
122
DLL_ENTRY
123
#endif
124
decode_instructions_virtual(uintptr_t start_va, uintptr_t end_va,
125
unsigned char* buffer, uintptr_t length,
126
event_callback_t event_callback_arg, void* event_stream_arg,
127
printf_callback_t printf_callback_arg, void* printf_stream_arg,
128
const char* options, int newline) {
129
struct hsdis_app_data app_data;
130
memset(&app_data, 0, sizeof(app_data));
131
app_data.start_va = start_va;
132
app_data.end_va = end_va;
133
app_data.buffer = buffer;
134
app_data.length = length;
135
app_data.event_callback = event_callback_arg;
136
app_data.event_stream = event_stream_arg;
137
app_data.printf_callback = printf_callback_arg;
138
app_data.printf_stream = printf_stream_arg;
139
app_data.do_newline = newline == 0 ? false : true;
140
141
return decode(&app_data, options);
142
}
143
144
/* This is the compatability interface for older version of hotspot */
145
void*
146
#ifdef DLL_ENTRY
147
DLL_ENTRY
148
#endif
149
decode_instructions(void* start_pv, void* end_pv,
150
event_callback_t event_callback_arg, void* event_stream_arg,
151
printf_callback_t printf_callback_arg, void* printf_stream_arg,
152
const char* options) {
153
return decode_instructions_virtual((uintptr_t)start_pv,
154
(uintptr_t)end_pv,
155
(unsigned char*)start_pv,
156
(uintptr_t)end_pv - (uintptr_t)start_pv,
157
event_callback_arg,
158
event_stream_arg,
159
printf_callback_arg,
160
printf_stream_arg,
161
options, false);
162
}
163
164
static void* decode(struct hsdis_app_data* app_data, const char* options) {
165
setup_app_data(app_data, options);
166
char buf[128];
167
168
{
169
/* now reload everything from app_data: */
170
DECL_EVENT_CALLBACK(app_data);
171
DECL_PRINTF_CALLBACK(app_data);
172
uintptr_t start = app_data->start_va;
173
uintptr_t end = app_data->end_va;
174
uintptr_t p = start;
175
176
(*event_callback)(event_stream, "insns", (void*)start);
177
178
(*event_callback)(event_stream, "mach name='%s'",
179
(void*) app_data->arch_info->printable_name);
180
if (app_data->dinfo.bytes_per_line != 0) {
181
(*event_callback)(event_stream, "format bytes-per-line='%p'/",
182
(void*)(intptr_t) app_data->dinfo.bytes_per_line);
183
}
184
185
while (p < end && !app_data->losing) {
186
(*event_callback)(event_stream, "insn", (void*) p);
187
188
/* reset certain state, so we can read it with confidence */
189
app_data->dinfo.insn_info_valid = 0;
190
app_data->dinfo.branch_delay_insns = 0;
191
app_data->dinfo.data_size = 0;
192
app_data->dinfo.insn_type = 0;
193
194
int size = (*app_data->dfn)((bfd_vma) p, &app_data->dinfo);
195
196
if (size > 0) p += size;
197
else app_data->losing = true;
198
199
if (!app_data->losing) {
200
const char* insn_close = format_insn_close("/insn", &app_data->dinfo,
201
buf, sizeof(buf));
202
(*event_callback)(event_stream, insn_close, (void*) p);
203
204
if (app_data->do_newline) {
205
/* follow each complete insn by a nice newline */
206
(*printf_callback)(printf_stream, "\n");
207
}
208
}
209
}
210
211
if (app_data->losing) (*event_callback)(event_stream, "/insns", (void*) p);
212
return (void*) p;
213
}
214
}
215
216
/* take the address of the function, for luck, and also test the typedef: */
217
const decode_func_vtype decode_func_virtual_address = &decode_instructions_virtual;
218
const decode_func_stype decode_func_address = &decode_instructions;
219
220
static const char* format_insn_close(const char* close,
221
disassemble_info* dinfo,
222
char* buf, size_t bufsize) {
223
if (!dinfo->insn_info_valid)
224
return close;
225
enum dis_insn_type itype = dinfo->insn_type;
226
int dsize = dinfo->data_size, delays = dinfo->branch_delay_insns;
227
if ((itype == dis_nonbranch && (dsize | delays) == 0)
228
|| (strlen(close) + 3*20 > bufsize))
229
return close;
230
231
const char* type = "unknown";
232
switch (itype) {
233
case dis_nonbranch: type = NULL; break;
234
case dis_branch: type = "branch"; break;
235
case dis_condbranch: type = "condbranch"; break;
236
case dis_jsr: type = "jsr"; break;
237
case dis_condjsr: type = "condjsr"; break;
238
case dis_dref: type = "dref"; break;
239
case dis_dref2: type = "dref2"; break;
240
case dis_noninsn: type = "noninsn"; break;
241
}
242
243
strcpy(buf, close);
244
char* p = buf;
245
if (type) sprintf(p += strlen(p), " type='%s'", type);
246
if (dsize) sprintf(p += strlen(p), " dsize='%d'", dsize);
247
if (delays) sprintf(p += strlen(p), " delay='%d'", delays);
248
return buf;
249
}
250
251
/* handler functions */
252
253
static int
254
hsdis_read_memory_func(bfd_vma memaddr,
255
bfd_byte* myaddr,
256
unsigned int length,
257
struct disassemble_info* dinfo) {
258
DECL_APP_DATA(dinfo);
259
/* convert the virtual address memaddr into an address within memory buffer */
260
uintptr_t offset = ((uintptr_t) memaddr) - app_data->start_va;
261
if (offset + length > app_data->length) {
262
/* read is out of bounds */
263
return EIO;
264
} else {
265
memcpy(myaddr, (bfd_byte*) (app_data->buffer + offset), length);
266
return 0;
267
}
268
}
269
270
static void
271
hsdis_print_address_func(bfd_vma vma, struct disassemble_info* dinfo) {
272
/* the actual value to print: */
273
void* addr_value = (void*) (uintptr_t) vma;
274
DECL_APP_DATA(dinfo);
275
DECL_EVENT_CALLBACK(app_data);
276
277
/* issue the event: */
278
void* result =
279
(*event_callback)(event_stream, "addr/", addr_value);
280
if (result == NULL) {
281
/* event declined */
282
generic_print_address(vma, dinfo);
283
}
284
}
285
286
287
/* configuration */
288
289
static void set_optional_callbacks(struct hsdis_app_data* app_data);
290
static void parse_caller_options(struct hsdis_app_data* app_data,
291
const char* caller_options);
292
static const char* native_arch_name();
293
static enum bfd_endian native_endian();
294
static const bfd_arch_info_type* find_arch_info(const char* arch_nane);
295
static bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
296
/* to avoid malloc: */
297
bfd* empty_bfd, bfd_target* empty_xvec);
298
static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
299
void *stream,
300
fprintf_ftype fprintf_func,
301
bfd* bfd,
302
char* disassembler_options);
303
static void parse_fake_insn(disassembler_ftype dfn,
304
struct disassemble_info* dinfo);
305
306
static void setup_app_data(struct hsdis_app_data* app_data,
307
const char* caller_options) {
308
/* Make reasonable defaults for null callbacks.
309
A non-null stream for a null callback is assumed to be a FILE* for output.
310
Events are rendered as XML.
311
*/
312
set_optional_callbacks(app_data);
313
314
/* Look into caller_options for anything interesting. */
315
if (caller_options != NULL)
316
parse_caller_options(app_data, caller_options);
317
318
/* Discover which architecture we are going to disassemble. */
319
app_data->arch_name = &app_data->mach_option[0];
320
if (app_data->arch_name[0] == '\0')
321
app_data->arch_name = native_arch_name();
322
app_data->arch_info = find_arch_info(app_data->arch_name);
323
324
/* Make a fake bfd to hold the arch. and byteorder info. */
325
struct {
326
bfd_target empty_xvec;
327
bfd empty_bfd;
328
} buf;
329
bfd* native_bfd = get_native_bfd(app_data->arch_info,
330
/* to avoid malloc: */
331
&buf.empty_bfd, &buf.empty_xvec);
332
init_disassemble_info_from_bfd(&app_data->dinfo,
333
app_data->printf_stream,
334
app_data->printf_callback,
335
native_bfd,
336
/* On PowerPC we get warnings, if we pass empty options */
337
(caller_options == NULL) ? NULL : app_data->insn_options);
338
339
/* Finish linking together the various callback blocks. */
340
app_data->dinfo.application_data = (void*) app_data;
341
app_data->dfn = disassembler(bfd_get_arch(native_bfd),
342
bfd_big_endian(native_bfd),
343
bfd_get_mach(native_bfd),
344
native_bfd);
345
app_data->dinfo.print_address_func = hsdis_print_address_func;
346
app_data->dinfo.read_memory_func = hsdis_read_memory_func;
347
348
if (app_data->dfn == NULL) {
349
const char* bad = app_data->arch_name;
350
static bool complained;
351
if (bad == &app_data->mach_option[0])
352
print_help(app_data, "bad mach=%s", bad);
353
else if (!complained)
354
print_help(app_data, "bad native mach=%s; please port hsdis to this platform", bad);
355
complained = true;
356
/* must bail out */
357
app_data->losing = true;
358
return;
359
}
360
361
parse_fake_insn(app_data->dfn, &app_data->dinfo);
362
}
363
364
365
/* ignore all events, return a null */
366
static void* null_event_callback(void* ignore_stream, const char* ignore_event, void* arg) {
367
return NULL;
368
}
369
370
/* print all events as XML markup */
371
static void* xml_event_callback(void* stream, const char* event, void* arg) {
372
FILE* fp = (FILE*) stream;
373
#define NS_PFX "dis:"
374
if (event[0] != '/') {
375
/* issue the tag, with or without a formatted argument */
376
fprintf(fp, "<"NS_PFX);
377
fprintf(fp, event, arg);
378
fprintf(fp, ">");
379
} else {
380
++event; /* skip slash */
381
const char* argp = strchr(event, ' ');
382
if (argp == NULL) {
383
/* no arguments; just issue the closing tag */
384
fprintf(fp, "</"NS_PFX"%s>", event);
385
} else {
386
/* split out the closing attributes as <dis:foo_done attr='val'/> */
387
int event_prefix = (argp - event);
388
fprintf(fp, "<"NS_PFX"%.*s_done", event_prefix, event);
389
fprintf(fp, argp, arg);
390
fprintf(fp, "/></"NS_PFX"%.*s>", event_prefix, event);
391
}
392
}
393
return NULL;
394
}
395
396
static void set_optional_callbacks(struct hsdis_app_data* app_data) {
397
if (app_data->printf_callback == NULL) {
398
int (*fprintf_callback)(FILE*, const char*, ...) = &fprintf;
399
FILE* fprintf_stream = stdout;
400
app_data->printf_callback = (printf_callback_t) fprintf_callback;
401
if (app_data->printf_stream == NULL)
402
app_data->printf_stream = (void*) fprintf_stream;
403
}
404
if (app_data->event_callback == NULL) {
405
if (app_data->event_stream == NULL)
406
app_data->event_callback = &null_event_callback;
407
else
408
app_data->event_callback = &xml_event_callback;
409
}
410
411
}
412
413
static void parse_caller_options(struct hsdis_app_data* app_data, const char* caller_options) {
414
char* iop_base = app_data->insn_options;
415
char* iop_limit = iop_base + sizeof(app_data->insn_options) - 1;
416
char* iop = iop_base;
417
const char* p;
418
for (p = caller_options; p != NULL; ) {
419
const char* q = strchr(p, ',');
420
size_t plen = (q == NULL) ? strlen(p) : ((q++) - p);
421
if (plen == 4 && strncmp(p, "help", plen) == 0) {
422
print_help(app_data, NULL, NULL);
423
} else if (plen >= 5 && strncmp(p, "mach=", 5) == 0) {
424
char* mach_option = app_data->mach_option;
425
size_t mach_size = sizeof(app_data->mach_option);
426
mach_size -= 1; /*leave room for the null*/
427
if (plen > mach_size) plen = mach_size;
428
strncpy(mach_option, p, plen);
429
mach_option[plen] = '\0';
430
} else if (plen > 6 && strncmp(p, "hsdis-", 6) == 0) {
431
// do not pass these to the next level
432
} else {
433
/* just copy it; {i386,sparc}-dis.c might like to see it */
434
if (iop > iop_base && iop < iop_limit) (*iop++) = ',';
435
if (iop + plen > iop_limit)
436
plen = iop_limit - iop;
437
strncpy(iop, p, plen);
438
iop += plen;
439
}
440
p = q;
441
}
442
*iop = '\0';
443
}
444
445
static void print_help(struct hsdis_app_data* app_data,
446
const char* msg, const char* arg) {
447
DECL_PRINTF_CALLBACK(app_data);
448
if (msg != NULL) {
449
(*printf_callback)(printf_stream, "hsdis: ");
450
(*printf_callback)(printf_stream, msg, arg);
451
(*printf_callback)(printf_stream, "\n");
452
}
453
(*printf_callback)(printf_stream, "hsdis output options:\n");
454
if (printf_callback == (printf_callback_t) &fprintf)
455
disassembler_usage((FILE*) printf_stream);
456
else
457
disassembler_usage(stderr); /* better than nothing */
458
(*printf_callback)(printf_stream, " mach=<arch> select disassembly mode\n");
459
#if defined(LIBARCH_i386) || defined(LIBARCH_amd64)
460
(*printf_callback)(printf_stream, " mach=i386 select 32-bit mode\n");
461
(*printf_callback)(printf_stream, " mach=x86-64 select 64-bit mode\n");
462
(*printf_callback)(printf_stream, " suffix always print instruction suffix\n");
463
#endif
464
(*printf_callback)(printf_stream, " help print this message\n");
465
}
466
467
468
/* low-level bfd and arch stuff that binutils doesn't do for us */
469
470
static const bfd_arch_info_type* find_arch_info(const char* arch_name) {
471
const bfd_arch_info_type* arch_info = bfd_scan_arch(arch_name);
472
if (arch_info == NULL) {
473
extern const bfd_arch_info_type bfd_default_arch_struct;
474
arch_info = &bfd_default_arch_struct;
475
}
476
return arch_info;
477
}
478
479
static const char* native_arch_name() {
480
const char* res = NULL;
481
#ifdef LIBARCH_i386
482
res = "i386";
483
#endif
484
#ifdef LIBARCH_amd64
485
res = "i386:x86-64";
486
#endif
487
#ifdef LIBARCH_sparc
488
res = "sparc:v8plusb";
489
#endif
490
#ifdef LIBARCH_sparcv9
491
res = "sparc:v9b";
492
#endif
493
#if defined(LIBARCH_ppc64) || defined(LIBARCH_ppc64le)
494
res = "powerpc:common64";
495
#endif
496
#ifdef LIBARCH_aarch64
497
res = "aarch64";
498
#endif
499
if (res == NULL)
500
res = "architecture not set in Makefile!";
501
return res;
502
}
503
504
static enum bfd_endian native_endian() {
505
int32_t endian_test = 'x';
506
if (*(const char*) &endian_test == 'x')
507
return BFD_ENDIAN_LITTLE;
508
else
509
return BFD_ENDIAN_BIG;
510
}
511
512
static bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
513
bfd* empty_bfd, bfd_target* empty_xvec) {
514
memset(empty_bfd, 0, sizeof(*empty_bfd));
515
memset(empty_xvec, 0, sizeof(*empty_xvec));
516
empty_xvec->flavour = bfd_target_unknown_flavour;
517
empty_xvec->byteorder = native_endian();
518
empty_bfd->xvec = empty_xvec;
519
empty_bfd->arch_info = arch_info;
520
return empty_bfd;
521
}
522
523
static int read_zero_data_only(bfd_vma ignore_p,
524
bfd_byte* myaddr, unsigned int length,
525
struct disassemble_info *ignore_info) {
526
memset(myaddr, 0, length);
527
return 0;
528
}
529
static int print_to_dev_null(void* ignore_stream, const char* ignore_format, ...) {
530
return 0;
531
}
532
533
/* Prime the pump by running the selected disassembler on a null input.
534
This forces the machine-specific disassembler to divulge invariant
535
information like bytes_per_line.
536
*/
537
static void parse_fake_insn(disassembler_ftype dfn,
538
struct disassemble_info* dinfo) {
539
typedef int (*read_memory_ftype)
540
(bfd_vma memaddr, bfd_byte *myaddr, unsigned int length,
541
struct disassemble_info *info);
542
read_memory_ftype read_memory_func = dinfo->read_memory_func;
543
fprintf_ftype fprintf_func = dinfo->fprintf_func;
544
545
dinfo->read_memory_func = &read_zero_data_only;
546
dinfo->fprintf_func = &print_to_dev_null;
547
(*dfn)(0, dinfo);
548
549
/* put it back */
550
dinfo->read_memory_func = read_memory_func;
551
dinfo->fprintf_func = fprintf_func;
552
}
553
554
static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
555
void *stream,
556
fprintf_ftype fprintf_func,
557
bfd* abfd,
558
char* disassembler_options) {
559
init_disassemble_info(dinfo, stream, fprintf_func);
560
561
dinfo->flavour = bfd_get_flavour(abfd);
562
dinfo->arch = bfd_get_arch(abfd);
563
dinfo->mach = bfd_get_mach(abfd);
564
dinfo->disassembler_options = disassembler_options;
565
dinfo->octets_per_byte = bfd_octets_per_byte (abfd);
566
dinfo->skip_zeroes = sizeof(void*) * 2;
567
dinfo->skip_zeroes_at_end = sizeof(void*)-1;
568
dinfo->disassembler_needs_relocs = FALSE;
569
570
if (bfd_big_endian(abfd))
571
dinfo->display_endian = dinfo->endian = BFD_ENDIAN_BIG;
572
else if (bfd_little_endian(abfd))
573
dinfo->display_endian = dinfo->endian = BFD_ENDIAN_LITTLE;
574
else
575
dinfo->endian = native_endian();
576
577
disassemble_init_for_target(dinfo);
578
}
579
580