0

In the DVM source code you can see methodIdx used as Method ID in a lot of functions. This attribute isn't in the Method Class and I didin't find a good description about it.

So my question is, is an absolut identifier, it's allways the same for a process, can change, when can change, etc... And how or where is generated.

Thanks

Marc
  • 129
  • 1
  • 12

1 Answers1

4

methodIdx is generally the "method reference index".

It's an index into the method_ids table, described in the file format docs. The index is relative to a single DEX file, which is why functions like dvmResolveMethod() take the referring class as well as the method reference index. It's mentioned in the invoke-kind explanation in the Dalvik bytecode doc.

Each method_id_item identifies a method by the class in which it was defined, and the function's prototype (name, arguments, return type). There is one method_id_item for every method declared in or referenced from a DEX file. The indices are established when the DEX file is generated by dx.

So... methodIdx gets you to a table entry with a bunch of strings. From there a symbolic lookup gets you to the actual method implementation, i.e. Method*. See dvmResolveMethod() in dalvik/vm/oo/Resolve.cpp for the details.

Because the invoke instructions only have room for a 16-bit method reference index, you can't refer to more than 65535 methods in a single DEX file. (This is a well known problem.)

Community
  • 1
  • 1
fadden
  • 51,356
  • 5
  • 116
  • 166
  • Thx! Nice answer. I can assume then that always with the same DEX each function has the same methodIdx, no? So for example I can use that number to identify the calls in a given dex file. – Marc May 05 '14 at 21:59
  • The pair { DEX file, methodIdx } resolves to a single specific method. A single method should not have more than one method_ids table entry (as that would be a waste of space), so there should not be two different methodIdx values that resolve to the same method within a single DEX. Note however that the same method, referenced from a different DEX file, could have a different methodIdx in the context of that DEX file. And if your DEX file is updated then everything can change. – fadden May 06 '14 at 00:07
  • So it can't be used as identifier. Thanks for your detailed explanation. – Marc May 06 '14 at 13:12
  • It can be used as part of an identifier within a running VM process, or for a static set of DEX files, so long as you pair it with a reference to the DEX file that defines the method. But no, the number alone isn't a globally-unique identifier. – fadden May 06 '14 at 14:38