def closest_symbol(addr_to_name, addr, max_offset=0x1000):
    """
    Find the symbol which is closest to addr, alongside with its offset.

    Returns:
        - (symbol_name, offset_to_symbol) if a symbol exists with an offset of a maximum of max_offset
        - Otherwise, (None, None) is returned in case no symbol with appropriate offset exists
    """
    if not addr_to_name:
        return (None, None)

    sorted_addrs = sorted(addr_to_name)
    for i, sym_addr in enumerate(sorted_addrs):
        # last entry?
        if i == len(sorted_addrs) - 1 or sorted_addrs[i+1] > addr:
            off = addr - sym_addr
            if 0 <= off <= max_offset:
                return addr_to_name[sym_addr], off
            return (None, None)
    return (None, None)