1 /*
2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #ifndef SHARE_VM_RUNTIME_RELOCATOR_HPP
26 #define SHARE_VM_RUNTIME_RELOCATOR_HPP
27 
28 #include "interpreter/bytecodes.hpp"
29 #include "oops/method.hpp"
30 #ifdef TARGET_ARCH_x86
31 # include "bytes_x86.hpp"
32 #endif
33 #ifdef TARGET_ARCH_aarch64
34 # include "bytes_aarch64.hpp"
35 #endif
36 #ifdef TARGET_ARCH_sparc
37 # include "bytes_sparc.hpp"
38 #endif
39 #ifdef TARGET_ARCH_zero
40 # include "bytes_zero.hpp"
41 #endif
42 #ifdef TARGET_ARCH_arm
43 # include "bytes_arm.hpp"
44 #endif
45 #ifdef TARGET_ARCH_ppc
46 # include "bytes_ppc.hpp"
47 #endif
48 
49 // This code has been converted from the 1.1E java virtual machine
50 // Thanks to the JavaTopics group for using the code
51 
52 class ChangeItem;
53 
54 // Callback object for code relocations
55 class RelocatorListener : public StackObj {
56  public:
RelocatorListener()57   RelocatorListener() {};
58   virtual void relocated(int bci, int delta, int new_method_size) = 0;
59 };
60 
61 
62 class Relocator : public ResourceObj {
63  public:
64   Relocator(methodHandle method, RelocatorListener* listener);
65   methodHandle insert_space_at(int bci, int space, u_char inst_buffer[], TRAPS);
66 
67   // Callbacks from ChangeItem's
68   bool handle_code_changes();
69   bool handle_widen       (int bci, int new_ilen, u_char inst_buffer[]);  // handles general instructions
70   void push_jump_widen  (int bci, int delta, int new_delta);    // pushes jumps
71   bool handle_jump_widen  (int bci, int delta);     // handles jumps
72   bool handle_switch_pad  (int bci, int old_pad, bool is_lookup_switch); // handles table and lookup switches
73 
74  private:
75   unsigned char* _code_array;
76   int            _code_array_length;
77   int            _code_length;
78   unsigned char* _compressed_line_number_table;
79   int            _compressed_line_number_table_size;
80   methodHandle   _method;
81   u_char         _overwrite[3];             // stores overwritten bytes for shrunken instructions
82 
83   GrowableArray<ChangeItem*>* _changes;
84 
code_array() const85   unsigned char* code_array() const         { return _code_array; }
set_code_array(unsigned char * array)86   void set_code_array(unsigned char* array) { _code_array = array; }
87 
code_length() const88   int code_length() const                   { return _code_length; }
set_code_length(int length)89   void set_code_length(int length)          { _code_length = length; }
90 
code_array_length() const91   int code_array_length() const             { return _code_array_length; }
set_code_array_length(int length)92   void set_code_array_length(int length)    { _code_array_length = length; }
93 
compressed_line_number_table() const94   unsigned char* compressed_line_number_table() const         { return _compressed_line_number_table; }
set_compressed_line_number_table(unsigned char * table)95   void set_compressed_line_number_table(unsigned char* table) { _compressed_line_number_table = table; }
96 
compressed_line_number_table_size() const97   int compressed_line_number_table_size() const               { return _compressed_line_number_table_size; }
set_compressed_line_number_table_size(int size)98   void set_compressed_line_number_table_size(int size)        { _compressed_line_number_table_size = size; }
99 
method() const100   methodHandle method() const               { return _method; }
set_method(methodHandle method)101   void set_method(methodHandle method)      { _method = method; }
102 
103   // This will return a raw bytecode, which is possibly rewritten.
code_at(int bci) const104   Bytecodes::Code code_at(int bci) const          { return (Bytecodes::Code) code_array()[bci]; }
code_at_put(int bci,Bytecodes::Code code)105   void code_at_put(int bci, Bytecodes::Code code) { code_array()[bci] = (char) code; }
106 
107   // get and set signed integers in the code_array
int_at(int bci) const108   inline int   int_at(int bci) const               { return Bytes::get_Java_u4(&code_array()[bci]); }
int_at_put(int bci,int value)109   inline void  int_at_put(int bci, int value)      { Bytes::put_Java_u4(&code_array()[bci], value); }
110 
111   // get and set signed shorts in the code_array
short_at(int bci) const112   inline short short_at(int bci) const            { return (short)Bytes::get_Java_u2(&code_array()[bci]); }
short_at_put(int bci,short value)113   inline void  short_at_put(int bci, short value) { Bytes::put_Java_u2((address) &code_array()[bci], value); }
114 
115   // get the address of in the code_array
addr_at(int bci) const116   inline char* addr_at(int bci) const             { return (char*) &code_array()[bci]; }
117 
instruction_length_at(int bci)118   int  instruction_length_at(int bci)             { return Bytecodes::length_at(NULL, code_array() + bci); }
119 
120   // Helper methods
align(int n) const121   int  align(int n) const                          { return (n+3) & ~3; }
code_slop_pct() const122   int  code_slop_pct() const                       { return 25; }
123   bool is_opcode_lookupswitch(Bytecodes::Code bc);
124 
125   // basic relocation methods
126   bool relocate_code         (int bci, int ilen, int delta);
127   void change_jumps          (int break_bci, int delta);
128   void change_jump           (int bci, int offset, bool is_short, int break_bci, int delta);
129   void adjust_exception_table(int bci, int delta);
130   void adjust_line_no_table  (int bci, int delta);
131   void adjust_local_var_table(int bci, int delta);
132   void adjust_stack_map_table(int bci, int delta);
133   int  get_orig_switch_pad   (int bci, bool is_lookup_switch);
134   int  rc_instr_len          (int bci);
135   bool expand_code_array     (int delta);
136 
137   // Callback support
138   RelocatorListener *_listener;
notify(int bci,int delta,int new_code_length)139   void notify(int bci, int delta, int new_code_length) {
140     if (_listener != NULL)
141       _listener->relocated(bci, delta, new_code_length);
142   }
143 };
144 
145 #endif // SHARE_VM_RUNTIME_RELOCATOR_HPP
146