Blame libdwfl/dwfl_module_getsrc.c

Packit Service 97d2fb
/* Find source location for PC address in module.
Packit Service 97d2fb
   Copyright (C) 2005, 2008, 2014 Red Hat, Inc.
Packit Service 97d2fb
   This file is part of elfutils.
Packit Service 97d2fb
Packit Service 97d2fb
   This file is free software; you can redistribute it and/or modify
Packit Service 97d2fb
   it under the terms of either
Packit Service 97d2fb
Packit Service 97d2fb
     * the GNU Lesser General Public License as published by the Free
Packit Service 97d2fb
       Software Foundation; either version 3 of the License, or (at
Packit Service 97d2fb
       your option) any later version
Packit Service 97d2fb
Packit Service 97d2fb
   or
Packit Service 97d2fb
Packit Service 97d2fb
     * the GNU General Public License as published by the Free
Packit Service 97d2fb
       Software Foundation; either version 2 of the License, or (at
Packit Service 97d2fb
       your option) any later version
Packit Service 97d2fb
Packit Service 97d2fb
   or both in parallel, as here.
Packit Service 97d2fb
Packit Service 97d2fb
   elfutils is distributed in the hope that it will be useful, but
Packit Service 97d2fb
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 97d2fb
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 97d2fb
   General Public License for more details.
Packit Service 97d2fb
Packit Service 97d2fb
   You should have received copies of the GNU General Public License and
Packit Service 97d2fb
   the GNU Lesser General Public License along with this program.  If
Packit Service 97d2fb
   not, see <http://www.gnu.org/licenses/>.  */
Packit Service 97d2fb
Packit Service 97d2fb
#ifdef HAVE_CONFIG_H
Packit Service 97d2fb
# include <config.h>
Packit Service 97d2fb
#endif
Packit Service 97d2fb
Packit Service 97d2fb
#include "libdwflP.h"
Packit Service 97d2fb
#include "../libdw/libdwP.h"
Packit Service 97d2fb
Packit Service 97d2fb
Dwfl_Line *
Packit Service 97d2fb
dwfl_module_getsrc (Dwfl_Module *mod, Dwarf_Addr addr)
Packit Service 97d2fb
{
Packit Service 97d2fb
  Dwarf_Addr bias;
Packit Service 97d2fb
  if (INTUSE(dwfl_module_getdwarf) (mod, &bias) == NULL)
Packit Service 97d2fb
    return NULL;
Packit Service 97d2fb
Packit Service 97d2fb
  struct dwfl_cu *cu;
Packit Service 97d2fb
  Dwfl_Error error = __libdwfl_addrcu (mod, addr, &cu);
Packit Service 97d2fb
  if (likely (error == DWFL_E_NOERROR))
Packit Service 97d2fb
    error = __libdwfl_cu_getsrclines (cu);
Packit Service 97d2fb
  if (likely (error == DWFL_E_NOERROR))
Packit Service 97d2fb
    {
Packit Service 97d2fb
      Dwarf_Lines *lines = cu->die.cu->lines;
Packit Service 97d2fb
      size_t nlines = lines->nlines;
Packit Service 97d2fb
      if (nlines > 0)
Packit Service 97d2fb
	{
Packit Service 97d2fb
	  /* This is guaranteed for us by libdw read_srclines.  */
Packit Service 97d2fb
	  assert(lines->info[nlines - 1].end_sequence);
Packit Service 97d2fb
Packit Service 97d2fb
	  /* Now we look at the module-relative address.  */
Packit Service 97d2fb
	  addr -= bias;
Packit Service 97d2fb
Packit Service 97d2fb
	  /* The lines are sorted by address, so we can use binary search.  */
Packit Service 97d2fb
	  size_t l = 0, u = nlines - 1;
Packit Service 97d2fb
	  while (l < u)
Packit Service 97d2fb
	    {
Packit Service 97d2fb
	      size_t idx = u - (u - l) / 2;
Packit Service 97d2fb
	      Dwarf_Line *line = &lines->info[idx];
Packit Service 97d2fb
	      if (addr < line->addr)
Packit Service 97d2fb
		u = idx - 1;
Packit Service 97d2fb
	      else
Packit Service 97d2fb
		l = idx;
Packit Service 97d2fb
	    }
Packit Service 97d2fb
Packit Service 97d2fb
	  /* The last line which is less than or equal to addr is what
Packit Service 97d2fb
	     we want, unless it is the end_sequence which is after the
Packit Service 97d2fb
	     current line sequence.  */
Packit Service 97d2fb
	  Dwarf_Line *line = &lines->info[l];
Packit Service 97d2fb
	  if (! line->end_sequence && line->addr <= addr)
Packit Service 97d2fb
	    return &cu->lines->idx[l];
Packit Service 97d2fb
	}
Packit Service 97d2fb
Packit Service 97d2fb
      error = DWFL_E_ADDR_OUTOFRANGE;
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  __libdwfl_seterrno (error);
Packit Service 97d2fb
  return NULL;
Packit Service 97d2fb
}
Packit Service 97d2fb
INTDEF (dwfl_module_getsrc)