Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a Java bytecode instruction: What does the number mean?

Tags:

java

bytecode

I was reading java bytecode and saw this:

getfield #5 (Field java.lang.String name)

What does #5 mean?

And how can I write a program in bytecode?

like image 246
Arvin Avatar asked Feb 17 '12 21:02

Arvin


Video Answer


2 Answers

Java class files and bytecode

Java class files (bytecode-files) is composed by different components:

http://en.wikipedia.org/wiki/Java_class_file

  • Magic Number: 0xCAFEBABE
  • Version of Class File Format: the minor and major versions of the class file
  • Constant Pool: Pool of constants for the class
  • (...)
  • Fields: Any fields in the class
  • Methods: Any methods in the class
  • Attributes: Any attributes of the class (for example the name of the sourcefile, etc.)

The number #5 simply refers to a location in the constant pool. And in that position a CONSTANT_FieldRef is found which contains a reference to a CONSTANT_NameAndType among other attributes. And CONSTANT_NameAndType contains a reference to a CONSTANT_Utf8 (which contains the actual string/name.)

So the flow looks like this:

getfield #number -> FieldRef -> NameAndType -> Utf8 -> string

http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html

So instead of saving a whole string in each getfield instruction a number is saved. This improves performance in the interpreter (or JIT) and space in the class file.

Hand-write bytecodes

Hand-written bytecodes can be assembled to a class file with this tool (it contains a lot of examples):

http://jasmin.sourceforge.net/

like image 69
Lasse Espeholt Avatar answered Oct 21 '22 09:10

Lasse Espeholt


The getfield instruction (IIRC) makes a reference into the class file's constant pool for information about what field should be looked up. The #5 here means "constant pool entry number 5," and this constant pool then contains information saying "look up the field name of type java.lang.String). The reason for this is that it keeps the size of the getfield instruction the same, regardless of the name or type of the field to look up.

I'm not sure I understand what you mean by "how can I write program in bytecode?" This is a pretty open-ended question; it's akin to asking how to write programs in any language, and requires a lot of learning. You may want to look into the Jasmin Java assembler, which can greatly simplify this process.

Hope this helps!

like image 21
templatetypedef Avatar answered Oct 21 '22 10:10

templatetypedef