+/* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is
+ returned and the corresponding PTR is set. */
+
+static int
+scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
+{
+ int arch_size, step, sect_size;
+ long dyn_tag;
+ CORE_ADDR dyn_ptr, dyn_addr;
+ gdb_byte *bufend, *bufstart, *buf;
+ Elf32_External_Dyn *x_dynp_32;
+ Elf64_External_Dyn *x_dynp_64;
+ struct bfd_section *sect;
+
+ if (abfd == NULL)
+ return 0;
+ arch_size = bfd_get_arch_size (abfd);
+ if (arch_size == -1)
+ return 0;
+
+ /* Find the start address of the .dynamic section. */
+ sect = bfd_get_section_by_name (abfd, ".dynamic");
+ if (sect == NULL)
+ return 0;
+ dyn_addr = bfd_section_vma (abfd, sect);
+
+ /* Read in .dynamic from the BFD. We will get the actual value
+ from memory later. */
+ sect_size = bfd_section_size (abfd, sect);
+ buf = bufstart = alloca (sect_size);
+ if (!bfd_get_section_contents (abfd, sect,
+ buf, 0, sect_size))
+ return 0;
+
+ /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
+ step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
+ : sizeof (Elf64_External_Dyn);
+ for (bufend = buf + sect_size;
+ buf < bufend;
+ buf += step)
+ {
+ if (arch_size == 32)
+ {
+ x_dynp_32 = (Elf32_External_Dyn *) buf;
+ dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
+ dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
+ }
+ else
+ {
+ x_dynp_64 = (Elf64_External_Dyn *) buf;
+ dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
+ dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr);
+ }
+ if (dyn_tag == DT_NULL)
+ return 0;
+ if (dyn_tag == dyntag)
+ {
+ /* If requested, try to read the runtime value of this .dynamic
+ entry. */
+ if (ptr)
+ {
+ struct type *ptr_type;
+ gdb_byte ptr_buf[8];
+ CORE_ADDR ptr_addr;
+
+ ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
+ ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8;
+ if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0)
+ dyn_ptr = extract_typed_address (ptr_buf, ptr_type);
+ *ptr = dyn_ptr;
+ }
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+/* Scan for DYNTAG in .dynamic section of the target's main executable,
+ found by consulting the OS auxillary vector. If DYNTAG is found 1 is
+ returned and the corresponding PTR is set. */
+
+static int
+scan_dyntag_auxv (int dyntag, CORE_ADDR *ptr)
+{
+ int sect_size, arch_size, step;
+ long dyn_tag;
+ CORE_ADDR dyn_ptr;
+ gdb_byte *bufend, *bufstart, *buf;
+
+ /* Read in .dynamic section. */
+ buf = bufstart = read_program_header (PT_DYNAMIC, §_size, &arch_size);
+ if (!buf)
+ return 0;
+
+ /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
+ step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
+ : sizeof (Elf64_External_Dyn);
+ for (bufend = buf + sect_size;
+ buf < bufend;
+ buf += step)
+ {
+ if (arch_size == 32)
+ {
+ Elf32_External_Dyn *dynp = (Elf32_External_Dyn *) buf;
+ dyn_tag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag, 4);
+ dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr, 4);
+ }
+ else
+ {
+ Elf64_External_Dyn *dynp = (Elf64_External_Dyn *) buf;
+ dyn_tag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag, 8);
+ dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr, 8);
+ }
+ if (dyn_tag == DT_NULL)
+ break;
+
+ if (dyn_tag == dyntag)
+ {
+ if (ptr)
+ *ptr = dyn_ptr;
+
+ xfree (bufstart);
+ return 1;
+ }
+ }
+
+ xfree (bufstart);
+ return 0;