How to find the number of entries in the .dynamic section of an ELF file?

Can there be a method determining the number of entries in the .dynamic section of an ELF file without using the readelf utility? I am trying to output data from the .dynamic section using the gelf_getdyn() function, but cannot figure out a way of programmatically finding the number of entries in this section. Any help or webpages that can help (cannot find the source code for readelf) would be extremely appreciative.