1 /* This test program is part of GDB, the GNU debugger.
3 Copyright 2011-2024 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Simulate loading of JIT code. */
32 #include "jit-protocol.h"
33 #include "jit-elf-util.h"
38 fprintf (stderr
, "Usage: jit-elf-main libraries...\n");
42 /* Must be defined by .exp file when compiling to know
43 what address to map the ELF binary to. */
45 #error "Must define LOAD_ADDRESS"
47 #ifndef LOAD_INCREMENT
48 #error "Must define LOAD_INCREMENT"
52 main (int argc
, char *argv
[])
56 /* Used as backing storage for GDB to populate argv. */
65 for (i
= 1; i
< argc
; ++i
)
68 void *load_addr
= (void *) (size_t) (LOAD_ADDRESS
+ (i
- 1) * LOAD_INCREMENT
);
69 printf ("Loading %s as JIT at %p\n", argv
[i
], load_addr
);
70 void *addr
= load_elf (argv
[i
], &obj_size
, load_addr
);
73 sprintf (name
, "jit_function_%04d", i
);
74 int (*jit_function
) (void) = (int (*) (void)) load_symbol (addr
, name
);
76 /* Link entry at the end of the list. */
77 struct jit_code_entry
*const entry
= calloc (1, sizeof (*entry
));
78 entry
->symfile_addr
= (const char *)addr
;
79 entry
->symfile_size
= obj_size
;
80 entry
->prev_entry
= __jit_debug_descriptor
.relevant_entry
;
81 __jit_debug_descriptor
.relevant_entry
= entry
;
83 if (entry
->prev_entry
!= NULL
)
84 entry
->prev_entry
->next_entry
= entry
;
86 __jit_debug_descriptor
.first_entry
= entry
;
89 __jit_debug_descriptor
.action_flag
= JIT_REGISTER
;
90 __jit_debug_register_code ();
92 if (jit_function () != 42)
94 fprintf (stderr
, "unexpected return value\n");
99 i
= 0; /* break before fork */
103 i
= 0; /* break after fork */
105 /* Now unregister them all in reverse order. */
106 while (__jit_debug_descriptor
.relevant_entry
!= NULL
)
108 struct jit_code_entry
*const entry
=
109 __jit_debug_descriptor
.relevant_entry
;
110 struct jit_code_entry
*const prev_entry
= entry
->prev_entry
;
112 if (prev_entry
!= NULL
)
114 prev_entry
->next_entry
= NULL
;
115 entry
->prev_entry
= NULL
;
118 __jit_debug_descriptor
.first_entry
= NULL
;
121 __jit_debug_descriptor
.action_flag
= JIT_UNREGISTER
;
122 __jit_debug_register_code ();
124 __jit_debug_descriptor
.relevant_entry
= prev_entry
;
128 return 0; /* break before return */