1cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng/* Find source location for PC address in module.
2cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Copyright (C) 2005, 2008 Red Hat, Inc.
3cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   This file is part of Red Hat elfutils.
4cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
5cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat elfutils is free software; you can redistribute it and/or modify
6cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   it under the terms of the GNU General Public License as published by the
7cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Free Software Foundation; version 2 of the License.
8cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
9cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat elfutils is distributed in the hope that it will be useful, but
10cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   WITHOUT ANY WARRANTY; without even the implied warranty of
11cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   General Public License for more details.
13cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
14cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   You should have received a copy of the GNU General Public License along
15cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   with Red Hat elfutils; if not, write to the Free Software Foundation,
16cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
17cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
18cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   In addition, as a special exception, Red Hat, Inc. gives You the
19cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   additional right to link the code of Red Hat elfutils with code licensed
20cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   under any Open Source Initiative certified open source license
21cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   (http://www.opensource.org/licenses/index.php) which requires the
22cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   distribution of source code with any binary distribution and to
23cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   distribute linked combinations of the two.  Non-GPL Code permitted under
24cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   this exception must only link to the code of Red Hat elfutils through
25cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   those well defined interfaces identified in the file named EXCEPTION
26cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   found in the source code files (the "Approved Interfaces").  The files
27cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   of Non-GPL Code may instantiate templates or use macros or inline
28cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   functions from the Approved Interfaces without causing the resulting
29cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   work to be covered by the GNU General Public License.  Only Red Hat,
30cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Inc. may make changes or additions to the list of Approved Interfaces.
31cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat's grant of this exception is conditioned upon your not adding
32cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   any new exceptions.  If you wish to add a new Approved Interface or
33cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   exception, please contact Red Hat.  You must obey the GNU General Public
34cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   License in all respects for all of the Red Hat elfutils code and other
35cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   code used in conjunction with Red Hat elfutils except the Non-GPL Code
36cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   covered by this exception.  If you modify this file, you may extend this
37cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   exception to your version of the file, but you are not obligated to do
38cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   so.  If you do not wish to provide this exception without modification,
39cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   you must delete this exception statement from your version and license
40cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   this file solely under the GPL without exception.
41cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
42cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat elfutils is an included package of the Open Invention Network.
43cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   An included package of the Open Invention Network is a package for which
44cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Open Invention Network licensees cross-license their patents.  No patent
45cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   license is granted, either expressly or impliedly, by designation as an
46cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   included package.  Should you wish to participate in the Open Invention
47cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Network licensing program, please visit www.openinventionnetwork.com
48cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   <http://www.openinventionnetwork.com>.  */
49cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
50cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#include "libdwflP.h"
51cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#include "../libdw/libdwP.h"
52cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
53cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben ChengDwfl_Line *
54cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Chengdwfl_module_getsrc (Dwfl_Module *mod, Dwarf_Addr addr)
55cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng{
56cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  Dwarf_Addr bias;
57cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  if (INTUSE(dwfl_module_getdwarf) (mod, &bias) == NULL)
58cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    return NULL;
59cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
60cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  struct dwfl_cu *cu;
61cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  Dwfl_Error error = __libdwfl_addrcu (mod, addr, &cu);
62cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  if (likely (error == DWFL_E_NOERROR))
63cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    error = __libdwfl_cu_getsrclines (cu);
64cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  if (likely (error == DWFL_E_NOERROR))
65cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    {
66cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      /* Now we look at the module-relative address.  */
67cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      addr -= bias;
68cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
69cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      /* The lines are sorted by address, so we can use binary search.  */
70cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      size_t l = 0, u = cu->die.cu->lines->nlines;
71cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      while (l < u)
72cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	{
73cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  size_t idx = (l + u) / 2;
74cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  if (addr < cu->die.cu->lines->info[idx].addr)
75cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	    u = idx;
76cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  else if (addr > cu->die.cu->lines->info[idx].addr)
77cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	    l = idx + 1;
78cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  else
79cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	    return &cu->lines->idx[idx];
80cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	}
81cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
82cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      if (cu->die.cu->lines->nlines > 0)
83cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	assert (cu->die.cu->lines->info
84cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng		[cu->die.cu->lines->nlines - 1].end_sequence);
85cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
86cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      /* If none were equal, the closest one below is what we want.
87cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	 We never want the last one, because it's the end-sequence
88cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	 marker with an address at the high bound of the CU's code.  */
89cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      if (u > 0 && u < cu->die.cu->lines->nlines
90cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  && addr > cu->die.cu->lines->info[u - 1].addr)
91cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	return &cu->lines->idx[u - 1];
92cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
93cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      error = DWFL_E_ADDR_OUTOFRANGE;
94cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    }
95cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
96cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  __libdwfl_seterrno (error);
97cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  return NULL;
98cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng}
99cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben ChengINTDEF (dwfl_module_getsrc)
100